PHP using Phpmailer with QQ mailbox Implementation email

Source: Internet
Author: User
Tags openssl version server port ssl connection using git

Because of the project needs, to achieve to our site users to send mail, so there is this blog. The following is a combination of online examples of my own practice. Hope to be of help to everyone.

Introduction of Phpmailer:

Advantages:

    1. Can be run on top of any platform

    2. Support for SMTP authentication

    3. Specify multiple recipients, CC address, send address and reply address when sending mail; Note: Add cc, BCC only under the win platform SMTP mode support

    4. Supports multiple message encodings including: 8bit,base64,binary and Quoted-printable

    5. Support for redundant SMTP servers, that is, you can specify the primary SMTP server address and can only specify the backup SMTP server

    6. Supports mail with attachments, and can add attachments in any format to your message-your server, of course, has a large enough bandwidth to support it.

    7. Custom message header information, which is similar to sending header information in PHP via the header function
      Support to make the message body HTML content, you can insert the diagram in the message body
      Chip

    8. Flexible Debug Support

    9. Tested and compatible SMTP servers include: Sendmail,qmail,postfix,imail,exchange, etc.

Phpmailer's acquisition:

Phpmailer Project Address: Https://github.com/PHPMailer/PHPMailer clone to local using git command, or click "Download ZIP" directly at the bottom right of the project page You can get the full Phpmailer code package and unzip it locally.

Step one: So that our QQ mailbox can send mail

How can I send a message here? In fact, our mailbox can send mail, but to achieve in our website to send mail, it is necessary to set up our QQ mailbox, because at this time our site is now as a third-party client exists.

650) this.width=650; "src=" http://img.blog.csdn.net/20160531235329721 "alt=" here write a picture describing "title=" "style=" border:none; Height:auto; "/>

    1. As we are going to use the SMTP server to send, it is recommended to open the previous two! When you click Open, it will prompt:

650) this.width=650; "src=" http://img.blog.csdn.net/20160531235758572 "alt=" here write a picture describing "title=" "style=" border:none; Height:auto; "/>

When you have completed the above steps, you will get an authorization code, you can copy it, we will use it later (open two words will get two authorization code, with the last authorization code!) Or click the Generate authorization code below to get a new authorization code, be sure to the latest! )。

Step two: Enable our PHP to use QQ mailbox to send mail

Phpmailer need PHP Socket extension support, and Phpmailer link QQ domain name mailbox need SSL encryption method, solid PHP also have OpenSSL support, can view phpinfo, the following two items are present can be used, Where the OpenSSL version number and so on, many virtual host PHP does not support the OpenSSL extension, then you may be tragic.

650) this.width=650; "src=" http://img.blog.csdn.net/20160601000527591 "alt=" here write a picture describing "title=" "style=" border:none; Height:auto; "/>

Step three: Phpmailer to do some processing

Since we have a lot of files in the Phpmailer folder that we downloaded, we don't need to waste the memory. We can thin this folder, here I only saved the following several files: class.phpmailer.php, class.phpmaileroauth.php, class.pop3.php, class.smtp.php, phpmailerautoload.php.
650) this.width=650; "src=" http://img.blog.csdn.net/20160601001125906 "alt=" here write a picture describing "title=" "style=" border:none; Height:auto; "/>

Step four: Write code to send mail

Here I am directly encapsulating a public method (written in the functions.php file):

/* Send mail method  * @param   $to: Recipient   $title: Title   $content: Message content  * @return  bool true: Send successful  false: Send failed  */function sendmail ($to, $title, $content) {    //introduced Phpmailer core file   using require_once contains warning     require_once to avoid duplicate definitions of Phpmailer classes ("phpmailer/class.phpmailer.php ");      require_once (" phpmailer/class.smtp.php ");     // Instantiate Phpmailer Core class      $mail  = new phpmailer ();     // Whether to enable debug for SMTP debugging   development environment recommended on   production environment comments Off   Default Debug debug mode off      $mail->smtpdebug  = 1;    //using SMTP authentication to send mail      $mail->issmtp ();     //SMTP need authentication   This must be true     $mail->smtpauth=true;    / /link QQ domain Mailbox server address      $mail->host =  ' smtp.qq.com ';     // Set the login authentication using SSL encryption      $mail->smtpsecure =  ' SSL ';     //set the remote server port number for the SSL connection to the SMTP server, the previous default is 25, But now the new seems to be out of use   optional 465 or 587     $mail->port = 465;    // Set the SMTP Helo message header   This optional   content arbitrary     //  $mail->helo =  ' hello  Smtp.qq.com server ';     //set the sender's host domain   optional   default to localhost  content any, it is recommended to use your domain name      $mail->hostname =  ' http://www.lsgogroup.com ';     // Set the encoding   optional gb2312  for sent messages I like utf-8  said UTF8 in some clients will be garbled      $mail->charset =   ' UTF-8 ';     //sets the sender's name (nickname)   Any content that displays the sender's name before the sender's mailbox address of the recipient's message     $ mail->fromname =  ' Lsgo lab ';     //smtp login account   Enter the QQ number in the string format       $mail->username = ' [email protected] ';     //SMTP login password   Use the generated authorization code (just the latest authorization code that you saved)      $mail->password =  ' Sqyofzbqlfkntbncl ';     //set sender email address   Fill in the above mentioned "sender's mailbox"      $mail->from =  ' [email protected] ';     //whether the message body is HTML encoded   Note here is a method   no longer an attribute  true or false     $mail->ishtml (true);      //Set Recipient Email address   This method has two parameters   The first parameter is the recipient's mailbox address   The second parameter is the nickname set for the address   Different mailbox systems will automatically handle changes   Here The second parameter doesn't mean much      $mail->addaddress ($to, ' LSGO online notification ');     //add multiple recipients   The method can be called multiple times     //  $mail->addaddress (' [email  Protected] ', ' LSGO online notification ')     //add the message topic      $mail->subject =   $title;     //Add message body   Set ishtml to True above, it can be a full HTML string   such as: Using File_get_ The contents function reads the local HTML file      $mail->body =  $content;     // Add an attachment to the message   The method also has two parameters   The first parameter is the directory where the attachment is stored (relative to the directory, or aFor directories)   The second parameter is the name of the attachment in the message attachment     //  $mail->addattachment ('./d.jpg ', ' mm.jpg ');     //also this method can be called multiple times   uploads several attachments     //  $mail->addattachment ('./ Jlib-1.1.0.js ', ' jlib.js ');     $status  =  $mail->send ();     //simple judgment and hint information     if ($status)  {         return true;    }else{        return  false;    }}

OK, now we call this public method to test, I want to send an email to [email protected] notify him to join Lsgo lab

<?phprequire_once ("./functions.php"); $flag = SendMail (' [email protected] ', ' LSGO online notification ', ' congratulations on your successful joining the Lsgo lab and start your learning journey! if ($flag) {echo] sent the message successfully! ";} else{echo "failed to send mail! ";}? >

Pro-Test success!!!


This article is from the "Storyteller under the Banyan Tree" blog, please be sure to keep this source http://woaijorden.blog.51cto.com/5299493/1956386

PHP using Phpmailer with QQ mailbox Implementation email

Related Article

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.