PHP uses QQ mailbox to send mail "phpmailer"

Source: Internet
Author: User
Tags imap server port ssl connection

In PHP application development, often need to verify the user mailbox, send message notification, and using the PHP built-in mail () function, you need the support of the mail system.

If you are familiar with the IMAP/SMTP protocol, you can write a mail-sending program with the Socket function, but it is not easy to develop such a program.

Fortunately , the Phpmailer package is strong enough to use it to send mail more easily, sparing us a lot of extra trouble .

Phpmailer

Phpmailer is a packaged PHP mail sending class, support for sending HTML content of e-mail, and can add attachments to send, not like the php itself mail () function requires server environment support, you only need to set up the mail server with relevant information to achieve the mail delivery function.

Phpmailer Project Address :Https://github.com/PHPMailer/PHPMailer

PHP extension Support

Phpmailer need PHP sockets extension support, and login QQ mailbox SMTP server must be encrypted by SSL, so PHP has to include OpenSSL support.


↑ Use the Phpinfo () function to view the socket and OpenSSL extension information (WAMP server has enabled the extension by default).

Phpmailer Core File


↑ only class.phpmailer.php and phpmailer/class.smtp.php are required in this document.

QQ Mailbox Settings

All major mailboxes support the SMTP protocol, but not all mailboxes are turned on by default, and you can open them manually in the settings of your mailbox.

Third-party services after providing the account and password, you can log in to the SMTP server, through which to control the way the mail transfer.

Turn on the SMTP service


↑ Select the IMAP/SMTP service and click on the service to open.

Verify the Secret Warranty


↑ Send SMS "Configure mail Client" to 1069-0700-69.

Get Authorization Code


↑SMTP Server Authentication password, need to be properly kept (PS: password directly without space).

PHP Basic code for sending messages

The following code demonstrates how Phpmailer is used, and note the configuration process for the Phpmailer instance.

<?PHP//introducing Phpmailer's core filerequire_once("phpmailer/class.phpmailer.php");require_once("phpmailer/class.smtp.php");//Instantiate Phpmailer Core class$mail=NewPhpmailer ();//whether to enable debug for SMTP$mail->smtpdebug = 1;//send mail using SMTP authentication$mail-issmtp ();//SMTP needs authentication This must be true$mail->smtpauth =true;//the server address of the link QQ domain mailbox$mail->host = ' smtp.qq.com ';//Set login authentication using SSL encryption$mail->smtpsecure = ' SSL ';//set the remote server port number for SSL connection to the SMTP server$mail->port = 465;//set the encoding for sent messages$mail->charset = ' UTF-8 ';//Set Sender Nickname$mail->fromname = ' Sender nickname ';//SMTP login account QQ mailbox can be$mail->username = ' [email protected] ';//The password for the SMTP login uses the generated authorization code$mail->password = ' ********** ';//set the sender's email address with the login account.$mail->from = ' [email protected] ';//whether the message body is HTML-encoded note here is a method$mail->ishtml (true);//Set Recipient email address$mail->addaddress (' [email protected] ');//adding multiple recipients calls the method multiple times $mail->addaddress (' [email protected] ');//Add the subject of the message$mail->subject = ' Mail Subject ';//Add message body$mail->body = ' ;//add an attachment to the message $mail->addattachment ('./example.pdf '));//Send mail$status=$mail->send ();
Encapsulation method

If you want to send mail directly using Phpmailer, you'll need to do a tedious configuration to do so much less efficiently.

To simplify the call process, I've made two footprints on top of it, and only need to configure the account, password, and nickname to customize your own Qqmailer class.

<?PHPrequire_once' Phpmailer/class.phpmailer.php ';require_once' Phpmailer/class.smtp.php ';classqqmailer{ Public Static $HOST= ' smtp.qq.com ';//the server address of QQ mailbox     Public Static $PORT= 465;//Remote server port number for SMTP server     Public Static $SMTP= ' SSL ';//log in using SSL encryption     Public Static $CHARSET= ' UTF-8 ';//set the encoding for sent messages    Private Static $USERNAME= ' [email protected] ';//Authorized login Account    Private Static $PASSWORD= ' **************** ';//password for authorized login    Private Static $NICKNAME= '??? ';//Email Nickname    /** * Qqmailer constructor. * @param bool $debug [debug mode]*/     Public function__construct ($debug=false)    {        $this->mailer =NewPhpmailer (); $this->mailer->smtpdebug =$debug? 1:0; $this-&GT;MAILER-&GT;ISSMTP ();//Send mail using SMTP method    }    /** * @return Phpmailer*/     Public functionGetmailer () {return $this-Mailer; }    Private functionLoadconfig () {/*Server Settings*/        $this->mailer->smtpauth =true;//Turn on SMTP authentication        $this->mailer->host = self::$HOST;//SMTP Server Address        $this->mailer->port = self::$PORT;//Remote server port number        $this->mailer->smtpsecure = self::$SMTP;//Login Authentication Method        /*Account Settings*/        $this->mailer->username = self::$USERNAME;//SMTP Login Account        $this->mailer->password = self::$PASSWORD;//SMTP Login Password        $this->mailer->from = self::$USERNAME;//Sender e-mail address        $this->mailer->fromname = self::$NICKNAME;//Sender Nickname (any content)        /*Content Setting*/        $this->mailer->ishtml (true);//whether the message body is HTML        $this->mailer->charset = self::$CHARSET;//The encoding of the sent message    }    /** * ADD attachment * @param $path [attachment path]*/     Public functionAddFile ($path)    {        $this->mailer->addattachment ($path); }    /** Send Email * @param $email [to] * @param $title [subject] * @param $content [body] * @return bool [hair Delivery status]*/     Public functionSend$email,$title,$content)    {        $this-Loadconfig (); $this->mailer->addaddress ($email);//Recipient Mailbox        $this->mailer->subject =$title;//Message Subject        $this->mailer->body =$content;//Mail information        return(BOOL)$this->mailer->send ();//Send mail    }}
qqmailer.php
require_once' Qqmailer.php ';//Instantiate Qqmailer$mailer=NewQqmailer (true);//Add an attachment$mailer->addfile (' 20130vl.jpg ');//message Header$title= ' Wish a heart, never to be absent. ‘;//Message Content$content= <<<EOF<p align= "center" >ai such as snow on the mountain, jiao if clouds between the moon. <br>Shu Jun has two meanings, so he will refuse. <br>today's Bucket party, Ming Dan Ditch head. <br>Myeongdong Station asasfor groove, ditch water things flow. <br>desolately desolately, marrying no need to cry. <br>willing to have a heart, never to be absent. <br>Bamboo Pole What curl, fish tail He Hing! <br>man is a heavy-blooded, what money knife! </p>EOF;//Send QQ Mail$mailer->send (' [email protected] ',$title,$content);
Test results

PHP uses QQ mailbox to send mail "phpmailer"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.