As a developer of PHP, there is often a need to add a Web site that uses its own domain name as the sender's e-mail address to automatically send messages, for example, to send users a verification code, notification information, and so on. For example: One of my users to register the module, you need to use [email protected] as the sender of the mail to the registered user to send a message containing the account activation link in HTML format, the validation of the user registration account is filled in the mailbox is the person's own.
So, how is this method of using your own domain name as the e-mail address for sending e-mails under PHP?
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 is PHP unable to send mail? The answer is no, if you are familiar with the POP3/SMTP protocol, combined with the socket function can write efficient and stable mail sender, but this workaround is equivalent to using PHP to write a POP3/SMTP client, for the general user is still difficult.
Fortunately, there are so many resources on the Internet that it is not phpmailer to be greeted.
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
- Add your own email address for the domain name mailbox, click "Member Management" in the "domain Mailbox" approved by the audit, click "Add Member", enter the email name under your domain name, then add QQ number (the QQ number is used as the user of the newly added mailbox. QQ is required to open the QQ mailbox and does not close the digital account number)
- 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 [I have a domain mailbox open in the domain mailbox currently 25 port is still can link, may be security policy, limit the new open domain mailbox account 25 Port ]), solid PHP also has the support of OpenSSL, Can view phpinfo, the following two items are available, including the OpenSSL version number, such as no tube; many web hosting PHP does not support OpenSSL extension, then you may be tragic.
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.
<?PHP
/ / Introduce the core file of PHPMailer Use require_once to contain warnings to avoid repeated definition of PHPMailer class
Require_once("phpmailer/class.phpmailer.php");
/ / Sample PHPMailer core class
$mail = new PHPMailer();
/ / Whether to enable smtp debug for debugging development environment is recommended to open the production environment comment out to disable the debug debug mode by default
$mail->SMTPDebug = 1;
/ / Use smtp authentication to send mail, of course you can choose pop mode sendmail way, etc. This article does not explain
/ / Can refer to the detailed introduction in http://phpmailer.github.io/PHPMailer/
$mail->isSMTP();
//smtp requires authentication. This must be true.
$mail->SMTPAuth=true;
/ / Link qq domain mailbox server address
$mail->Host = ‘smtp.qq.com‘;
/ / Set to use ssl encryption to log in authentication
$mail->SMTPSecure = ‘ssl‘;
/ / Set ssl connection smtp server remote server port number Optional 465 or 587
$mail->Port = 465;
/ / Set smtp helo message header This is optional
$mail->Helo = ‘Hello smtp.qq.com Server’;
/ / Set the sender's host domain can be optional No default to localhost content, it is recommended to use your domain name
$mail->Hostname = ‘jjonline.cn‘;
/ / Set the encoding of the sent mail Optional GB2312 I like utf-8 It is said that utf8 will be garbled under some client receiving
$mail->CharSet = ‘UTF-8‘;
/ / Set the sender's name (nickname) Any content, the sender's name is displayed before the sender's email address of the recipient's email
$mail->FromName = ‘Jingjing Online’;
//smtp login account here fill in the qq number in string format
$mail->Username = '888888';
//smtp login password Enter "individual password" here. If you set "independent password", fill in the password for logging in qq. It is recommended to set "independent password".
$mail->Password = ‘xxxxxxx‘;
//Set the sender's email address. Fill in the "Sender's Mailbox" mentioned above.
$mail->From = ‘[email protected]‘;
/ / Is the mail body html encoding? Note that this is a method is no longer attribute true or false
$mail->isHTML(true);
/ / Set the recipient's email address. This method has two parameters. The first parameter is the recipient's email address. The second parameter is the nickname set for the address. The different mailbox system will automatically process the change. The meaning of the second parameter here. Not big
$mail->addAddress(‘[email protected]‘, ’ Jingjing Online User’);
/ / Add multiple recipients, you can call the method multiple times
$mail->addAddress(‘[email protected]‘, ’ Jingjing Online User’);
//Add the subject of the message
$mail->Subject = ‘PHPMailer sends an example of a message’;
//Add the body of the message. Set isHTML to true above, then 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 <b style=\"color:red;\">PHPMailer</b> a test case for sending mail";
//Add an attachment to the message. This method also has two parameters. The first parameter is the directory where the attachment is stored (relative directory, or absolute directory). The second parameter is the name of the attachment in the email attachment.
$mail->addAttachment(‘./d.jpg‘,‘mm.jpg‘);
/ / The same method can be called multiple times to upload multiple attachments
$mail->addAttachment(‘./Jlib-1.1.0.js‘, ‘Jlib.js‘);
//send command to return boolean
//PS: After testing, if the recipient does not exist, it will return true if there is no error. That is, before sending, you need some methods to detect whether the mailbox is authentic or not.
$status = $mail->send();
/ / Simple judgment and prompt information
If($status) {
Echo ‘Send mail successfully’;
}else{
Echo ‘Send mail failed, error message is 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 my server uses CentOS, not tested
PHP under the use of Phpmailer with QQ mailbox under the domain name mailbox to send mail