PHP uses the PHPMailer class library to implement the QQ mail sending method

Source: Internet
Author: User
Tags openssl version account security create domain email account ssl connection
Php Chinese network ( provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming... In fact, the PHP mail sending function is not always available for every project, but in fact every complete project generally contains a PHP mail sending function.

The Mail sending function is generally used for registration activation, password retrieval, and message reply. This is also a headache for many people. after all, this function is not implemented by several lines of code, and it is not written by you with time and energy. This function is actually a finished product. now we need to use it. next we will explain how to use the PHPMailer class library to implement the PHP mail sending function.

First of all, let's talk about how to use your own domain name as the mail address for sending emails?

In the PHP environment, the mail () function is provided. However, this function requires the server to support sendmail or a mail sending server without relay, however, it is almost impossible to find an email to send relay without authentication. Therefore, the mail function is often unable to send emails successfully. So php cannot send emails? The answer is No. if you are familiar with the POP3/SMTP protocol and use the socket function, you can compile an efficient and stable mail sending program, however, this work ing method is equivalent to compiling a POP3/SMTP client using php, which is still difficult for general developers.

Fortunately, many Internet resources are available, and PHPMailer was born at this time.

What are the advantages of PHPMailer?

Can run on any platform

Support for SMTP verification

Multiple recipients, CC addresses, and reply addresses are specified during mail sending. note: only support for smtp mode in win platform

Supports multiple email codes, including 8bit, base64, binary, and quoted-printable.

Supports redundant SMTP servers, that is, you can specify the master smtp server address or only the backup smtp server address.

Emails with attachments are supported, and attachments in any format can be added to the emails. of course, your server has enough bandwidth to support the emails.

Customize the mail header information, which is similar to the header information sent through the header function in php.

You can insert an image into the body of the email as HTMl content.

Flexible debug support

Compatible SMTP servers include Sendmail, qmail, Postfix, Imail, and Exchange.

How do I download the PHPMailer code package?

PHPMailer Project address: the uses the git command to clone to the local, or directly click Download ZIP in the lower right of the project page to get the complete PHPMailer code package.

How can I use PHPMailer and "domain name mailbox" in my QQ mailbox to send emails using my own domain name as the email address?


The following sections describe how to use your own domain name to activate the "domain name mailbox" under the QQ mailbox for free, and use PHPMailer in the PHP environment, and use it with the newly activated "domain name mailbox" to automatically send emails.


Part 1: activate the "domain name mailbox" under the QQ mailbox"

Because the activation of "domain name mailbox" is only an understanding of the operation method, this part is only for a brief introduction.

Go to QQ mail, click "settings" in the upper-left corner of the page, click the "account" tab under "mailbox settings", and drag the scroll bar to see "domain name Mailbox ", click "Manage domain name mailbox" to enter the domain name mailbox added page; or directly click

Click "create domain name mailbox" and follow the prompts on the page to add and verify your domain name. PS: If "domain name mailbox" is successfully added to your QQ mailbox, after entering the QQ mailbox, after the "set" button in the upper-left corner of the page, the "domain name mailbox" text will add your own exclusive email address to the domain name mailbox. click "member management" in the "domain name mailbox" that is approved ", click "Add Member", enter the desired mailbox name under your domain name, and then add a QQ number (this QQ number is used as the user of the newly added mailbox. the QQ number must be activated and no digital account is disabled)

The QQ mailbox that just entered the QQ number will receive a notification email indicating that the domain name has been activated. click "accept this email account" in the email and use PHPMailer to set the sender's email address, to facilitate the description of this email address, it is called the "sender email ".

To ensure the security of the logon password for this qq account, set "independent password" for this qq account, and set "settings"-"account"-"account security" in the qq mailbox, the password set here is called an "independent password" for convenience described in this article ".

At this point, the "domain name mailbox" under the QQ mailbox has been activated and added the e-mail address that will be sent using PHPMailer as the sender. The following lists the key information to be used by PHPMailer: QQ number. the QQ number, "independent password", "sender email", and sender name added in step 3...

Part 2: Use PHPMailer to write the email sending code

PHPMailer requires PHP socket extension support, while PHPMailer requires ssl encryption when linking qq domain name mailbox (qq mailbox has recently been limited, the newly opened domain name mailbox no longer allows the normal link through the smtp protocol port 25, only port 465 or port 587 of ssl is allowed. [currently, port 25 of the domain name mailbox opened in one of my domain name mailboxes in the beta period can be linked. This may be a security policy, limit the port 25 of the new domain name mailbox account]). fixed php must support openssl. you can view phpinfo. you can use the following two items if they exist. you do not need to worry about the openssl version; php in many virtual hosts does not support openssl extension, so you may be miserable.

Before that, you must enable openssl in the php environment.

This part only serves as an example of the code, and some comments and explanations are provided in the code.

After the downloaded PHPMailer is decompressed, you only need class. phpmailer. php, class. pop3.php, class. smtp. php and PHPMailerAutoload. four php files, which can be used for the language folder or not, are mainly used for display information during debug debugging. Check whether the file size of class. phpmailer. php is more than KB, which is quite scary.

 SMTPDebug = 1; // use smtp Authentication to send an email. of course, you can use the pop sendmail method. This article will not elaborate. // you can refer $ Mail-> isSMTP (); // for smtp authentication, the value must be true $ mail-> SMTPAuth = true; // link the server address of the qq domain name email address $ mail-> Host = 'smtp '; // Set logon authentication using ssl encryption $ mail-> SMTPSecure = 'SSL '; // Set the remote server Port number for ssl connection to the smtp server. optional values: 465 or 587 $ mail-> Port = 465; // Set the smtp helo message header, which can have no content. $ mail-> Helo = 'Hello server'; // you can set the host domain of the sender without the default localhost content, we recommend that you use your domain name $ mail-> Hostname = 'jjonline. cn '; // Set the encoding of the sent email. optional. GB2312. I like UTF-8. Some clients may receive emails with garbled characters $ mail-> CharSet = 'utf-8'; // you can specify any sender name (nickname, the sender's name before the recipient's email address is displayed $ mail-> FromName = 'jingjing Online '; // enter the QQ number in string format for the smtp logon account $ mail-> Username = '123 '; // enter the smtp logon Password here as "independent Password". if "independent Password" is set, enter the logon qq Password. we recommend that you set "independent Password" $ mail-> Password = 'xxxxxxx '; // Set the sender's email address. enter "sender's email address" $ mail-> From = 'register @ '; // whether the email body is html encoded. Note that a method is no longer a property of true or false $ mail-> isHTML (true ); // Set the recipient's email address. this method has the first two parameters. The parameter is the recipient's email address. The second parameter is the nickname set for this address. different email addresses are automatically changed. // The second parameter is of little significance. $ mail-> addAddress ('XXX @ ', 'jingjing online user'); // add multiple recipients to call the method multiple times. $ mail-> addAddress ('XXX @ ', 'jingjing online user '); // add the Subject of the email $ mail-> Subject = 'example of an email sent by PHPMailer '; // Set isHTML to true at the top of the email body, it can be a complete html string // for example: Use the file_get_contents function to read the local html file $ mail-> Body = "this is a test case for PHPMailer to send emails "; // add an attachment to the email. this method also has two parameters. The first parameter is the directory where the attachment is stored (either relative directory or absolute directory) the second parameter is the name of the attachment in the email attachment $ Mail-> addAttachment ('. /d.jpg','mm.jpg '); // This method can also be called multiple times to upload multiple attachments $ mail-> addAttachment ('. /Jlib-1.1.0.js ', 'jlib. js '); // boolean value returned by the sending command // PS: After testing, if the recipient does not exist, if no error is returned, true is returned. that is, you need some methods to check whether the email address is valid before sending the email. $ status = $ mail-> send (); // simple judgment and prompt message if ($ status) {echo 'email sent successfully';} else {echo 'email failed, error message not :'. $ mail-> ErrorInfo;}?>

Note: The above code snippet is for reference only. to integrate it into the website system, you can encapsulate it into a function or class library for calling; in addition, PHPMailer supports CC and BCC on the win32 Platform. Because centos is used on your servers, it is 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: 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.