PHP using Phpmailer with QQ mailbox Implementation email

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

Objective:

Because of the needs of the job, to achieve to our website 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.

    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:

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.

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.

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 succeeded false: Send failed */function sendMail ($to,$title,$content) {The core file that introduces Phpmailer uses require_once to contain warnings to avoid duplicate definitions of Phpmailer classes require_once ("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 It is recommended to turn on the production environment comment off to turn debug debug mode off by default$mail->smtpdebug=1;Send mail using SMTP authentication$mail->ISSMTP ();SMTP needs authentication This must be true$mail->smtpauth=TrueThe server address of the link QQ domain mailbox$mail->host=' Smtp.qq.com ';Set login authentication using SSL encryption$mail->smtpsecure=' SSL ';Set the SSL connection to the SMTP server's remote server port number, the previous default is 25, but now it seems that the new is not available for optional 465 or 587$mail->port=465;Set SMTP Helo message header This optional content is arbitrary$mail->helo = ' Hello smtp.qq.com Server ';Setting the sender's host domain is optional by default to localhost content, it is recommended to use your domain name$mail->hostname=' Http://www.lsgogroup.com ';Set the encoding of sent messages optional GB2312 I like utf-8. UTF8 is said to be garbled in some client's inbox.$mail->charset=' UTF-8 ';Set the sender's name (nickname) any content that displays the sender's name before the sender's e-mail address of the recipient's message$mail->fromname=' Lsgo laboratory ';SMTP login account here to fill in the string format QQ number can be$mail->username=' [email protected] ';The SMTP login password uses the generated authorization code (just the latest authorization code that you saved)$mail->password=' Sqyofzbqlfkntbncl ';Set the sender's e-mail address here fill in the "sender's mailbox" mentioned above$mail->from=' [email protected] ';Whether the message body is HTML-encoded note here is a method that is no longer a property of true or False$mail->ishtml (true);To set a recipient's mailbox address this method has two parameters the first parameter is the recipient's mailbox address the second parameter is a nickname that is set to that address. The second parameter has a small meaning.$mail->addaddress ($to,' Lsgo online notice ');Adding multiple recipients calls the method multiple times$mail->addaddress (' [email protected] ', ' lsgo online notice ');Add the subject of the message$mail->subject=$title;Adding ishtml to true on top of the message body can be a complete HTML string such as: Use the file_get_contents function to read 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 (either relative to the directory, or the absolute directory) The second parameter is the name of the attachment in the message attachment //$mail->addattachment ('./d.jpg ', ' mm.jpg '); //the same method can be called multiple times to upload more than one attachment //$mail->addattachment ('./ Jlib-1.1.0.js ', ' jlib.js ');  $status =  $mail ->send (); //simple judgment and hint information if ( $status) { Span class= "Hljs-keyword" >return true;} else{return false;}}    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21st
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21st
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82

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在线通知‘,‘恭喜你成功加入LSGO实验室,开启你的学习之旅吧!‘);if($flag){ echo "发送邮件成功!";}else{ echo "发送邮件失败!";}?>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

Hope this blog can help everyone.

PHP using Phpmailer with QQ mailbox Implementation email

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.