PHP through the Phpmailer class library to implement the QQ mailbox delivery method

Source: Internet
Author: User
Tags openssl version account security server port ssl connection using git
In fact, the PHP mailbox send function Not every project must be used to get, but in fact, each complete project, generally will contain a PHP mailbox send function.


General mailbox send function is commonly used for registration activation, password retrieval, and message reply and so on functions. And this is a headache for many people, after all, this function is not a few lines of code can be achieved, and it is not your time and energy to write it. And this function is actually a finished product, now need to let us how to use it, then we will explain how to use the Phpmailer class library to implement the PHP mailbox send function.



First of all, let's say how to use your own domain name as the e-mail address of the way to implement it?


PHP environment, is to provide the mail to send the function mail (), but the function requires the server to support SendMail or must set up a mail sending server that does not need to relay, but now to find a message that does not require authentication to send relay is almost impossible, Therefore, using the mail function often fails to send e-mail successfully. So PHP can't send mail? The answer is no, if you are familiar with the POP3/SMTP protocol, combined with the socket function can write a highly efficient and stable mail-sending program, but this workaround is equivalent to using PHP to write a POP3/SMTP client, It is still difficult for the general developer.


But fortunately now the Internet resources are very rich, this time Phpmailer was born.


What are the advantages of Phpmailer?

Can be run on top of any platform

Support for SMTP authentication

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

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

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

Support for mail with attachments, you can add any format of attachments to the message---of course your server has enough bandwidth support

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 a picture in the body of the message

Flexible Debug Support

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


How do I download the Phpmailer code package?

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 To get to the full Phpmailer code package.


How to use Phpmailer with the "domain Mailbox" under the QQ mailbox to use your own domain name as email address to send mail?

====

The following two parts, explain the use of their own domain name free to open the "domain Mailbox" under the QQ Mailbox and PHP environment using Phpmailer and with the newly opened "Domain name mailbox" Automatically send mail.

====


The first part: opening the "domain Mailbox" under the QQ mailbox


Since the opening of the "domain Mailbox" is only an understanding of how to operate, this part is only a brief talk.


Enter the QQ mailbox, click on the top left of the page "settings", and then click on the "Mailbox settings" under the "Account" tab, drag the scroll bar, you can see the "Domain Mailbox", click "Manage Domain Name mailbox" To enter the domain name mailbox added pages, or directly click http://domain.mail.qq.com/


Click "Create a Domain name mailbox" and follow the page prompts to add your own domain name and verify it. PS: If your QQ mailbox successfully added a "Domain name mailbox", then enter the QQ mailbox, the top left of the page "settings" button will have "domain name mailbox" text for the domain name mailbox to add their own email address, click on the "Domain mailbox" approved by "member management", click "Add members", Enter your own domain name under the favorite mailbox name, and then add QQ number (the QQ number as the user of the newly added mailbox, the QQ number needs to open QQ mailbox and not close the digital account)


Just entered the QQ number of QQ mailbox will receive an open domain mailbox notification mail, in the mail click "Accept this mailbox account", the use of phpmailer Send mail sender email address is set to complete, in order to facilitate the description of this email address called "Sender mailbox."


For the QQ number of password security, please set the "Independent password" for the QQ, in the QQ mailbox "set"-"account"-"account security" to set up the password, in order to facilitate the description of this article, called "Independent password."


At this point, the "domain Mailbox" under the QQ mailbox is opened and added to the email address that will be sent as the sender using Phpmailer. Here are a few key information to be used by Phpmailer: QQ number, QQ number added in the third step above, "Independent password", "Sender Mailbox", Sender's name ...


Part II: Writing code to send mailboxes using Phpmailer


Phpmailer need PHP Socket extension support, and Phpmailer link QQ domain name mailbox need SSL encryption Method (QQ mailbox has recently limited, new open domain mailbox no longer allow 25 port through the SMTP protocol of the normal link way link, Only allow SSL 465 or 587 ports [my one domain mailbox open in the domain mailbox at present 25 port is still can link, may be security policy, limit the new open domain mailbox account 25 port]), solid PHP also have the support of OpenSSL, can view phpinfo, The following two items are available, including the OpenSSL version number and so on, many virtual host PHP is not support OpenSSL extension, then you may be tragic.


It's important to note that before we do this, we have to open OpenSSL in a PHP environment.


This section is for code examples only, and some comments are explained in the code.


Download the Phpmailer after decompression to thin body, only need class.phpmailer.php, class.pop3.php, class.smtp.php and phpmailerautoload.php four files, language folder can not be, that is mainly used for debug debugging display information. Look at the file size of class.phpmailer.php, 110kb, quite scary.

 Smtpdebug = 1; Use SMTP authentication method to send mail, of course you can choose Pop mode SendMail way and so on not to do the detailed//can refer to http://phpmailer.github.io/PHPMailer/in the details of the introduction $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 the login authentication using SSL encryption $mail->smtpsecure = ' SSL ';     Set the remote server port number for the SSL connection to the SMTP server optional 465 or 587 $mail->port = 465;     Set SMTP Helo message header This optional content 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 = ' jjonline.cn ';     Set encoding for sent messages optional GB2312 I like Utf-8 said UTF8 in some clients will be garbled $mail->charset = ' UTF-8 ';     Set the sender's name (nickname) any content that displays the sender's name before the sender's email address of the recipient's message $mail->fromname = ' jingjing online ';     SMTP login account here to fill in the string format QQ number can $mail->username = ' 888888 ';     The password for the SMTP login is filled in with "Independent password" if the password is set "Independent password" is entered in the Login QQ passwords recommended setting "Independent password" $mail->password = ' xxxxxxx ';     Set the sender's email address here fill in the "sender's mailbox" mentioned above $mail->from = ' register@jjonline.cn '; 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 different mailbox system that is set to that address. The meaning of the second parameter is not very $mail->addaddress (' xxx@qq.com '     , ' jingjing online users ');     Adding multiple recipients calls the method multiple times $mail->addaddress (' xxx@163.com ', ' jingjing online users ');     Add the subject of the message $mail->subject = ' Phpmailer example of sending mail '; Add the message body above set ishtml to True, it can be a complete HTML string//such as: Use the file_get_contents function to read the local HTML file $mail->body = "This is a phpmailer send mail     One of the test cases ";     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 ');     The Send command returns a Boolean value of//ps: tested, if the recipient does not exist, if the error does not appear to still return true that is, before sending itself to implement a method to detect whether the mailbox is real and valid $status = $mail->send ();    Simple judgment and Cue message if ($status) {echo ' sent the message successfully ';    }else{Echo ' failed to send message, error message not: '. $mail->errorinfo; }?>

Special Note: The above code snippet is for reference only, in order to integrate into the site system, you can encapsulate the code snippet into a function or class library for invocation, and Phpmailer in the Win32 platform is supported by the addition of CC, Dark send. Because the server used CentOS, not tested.

  • 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.