Recently done a mailbox verification function, studied for a while, took care of the automatic sending of mail. The following with QQ mailbox as a demonstration, step by step to explain:
Code
First of all, is to do mail delivery, the code is as follows:
[HTML]View PlainCopy
- <? PHP
- Send mail
- Require './mailer/class.phpmailer.php ';
- Require './mailer/class.smtp.php ';
- Date_default_timezone_set (' PRC ');//Set the time for the message to be sent and, if not set, the time of the other zone
- $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 = 3;
- Use SMTP authentication method to send mail, of course you can choose Pop mode SendMail way and so on do not explain in this article
- Refer to the http://phpmailer.github.io/PHPMailer/for more information
- $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 login authentication using SSL encryption
- $mail->smtpsecure = ' SSL ';
- To set the remote server port number for an SSL connection to an SMTP server optional 465 or 587
- $mail->Port = 465;
- Set Sender's host domain optional default to localhost content arbitrary, recommended to use your domain name, here is the default localhost
- $mail->Hostname = ' localhost ';
- To set the encoding of sent messages optional GB2312
- $mail->CharSet = ' UTF-8 ';
- Set sender name (nickname) can be any content, do not affect the reply (set as QQ nickname can be)
- $mail->fromname = ' XXXX ';
- SMTP login account here to fill in QQ number can be
- $mail->Username =' qq number of the sender ';
- The password for the SMTP login is filled in with "Independent password", if the password is set "Independent password", then fill in QQ passwords recommended setting "Independent password"
- $mail->Password = ' QQ email login password ';
- Set the sender's e-mail address here fill in the "sender's mailbox" mentioned above
- $mail->from = ' sender's QQ mailbox ';
- Whether the message body is sent in HTML
- $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 (' recipient's QQ email address ', ' QQ nickname ');
- Multiple recipients can be added
- $mail->addaddress (' [email protected] ', ' XXXXX ');
- Add the subject of the message
- $mail->Subject = ' This is an example of a phpmailer sending mail ';
- Add Message body
- $mail->Body = "This is a <b style=\"color:red;\ ">phpmailer</b> A test case for sending a message ";
- 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 to the absolute directory)
- The second parameter is the name of the attachment in the message attachment
- $mail->addattachment ('./src/20151002.png ', ' test.png ');
- The same method can be called multiple times to upload several attachments
- $mail->addattachment ('./jlib-1.1.0.js ', ' jlib.js ');
- Send command returns a Boolean value
- PS: Tested, if the recipient does not exist. Returns true if no error occurs, which means that before sending
- I need some method to detect whether the mailbox is real and effective
- $Status = $mail->send ();
- Simple judgment and prompt information
- if ($status)
- {
- Echo ' Send mail Success '. Date (' y-m-d h:i:s ');;
- }
- Else
- {
- Echo ' failed to send message, error message: '. $mail->ErrorInfo;
- }
- ?>
This will enable the delivery of the message. The results are as follows:
If you want to automate the sending of messages, the program must be able to run automatically. The code for the program to run automatically in the background is as follows:
[HTML]View PlainCopy
- <? PHP
- Ignore_user_abort (); Running in the background
- Set_time_limit (0); Maximum timeout for canceling script run time
- $interval=60*30;//run every half hour, this interval can be modified as needed
- do{
- XXXXX//Here is the code you want to execute
- Sleep ($interval); Sleep for half an hour
- }while (TRUE);
- ?>
By combining the code that sends the message with the code that runs automatically, you can automate the message delivery:
[HTML]View PlainCopy
- <? PHP
- Send mail
- Require './mailer/class.phpmailer.php ';
- Require './mailer/class.smtp.php ';
- Date_default_timezone_set (' PRC ');
- Ignore_user_abort ();//Background run
- Set_time_limit (0);//Cancel the timeout limit for script run time
- $interval = 60*1;//run once every minute (this can be adjusted as needed)
- do{
- $mail = new Phpmailer ();
- $mail->smtpdebug = 3;
- $mail->issmtp ();
- $mail->smtpauth=true;
- $mail->Host = ' smtp.qq.com ';
- $mail->smtpsecure = ' SSL ';
- $mail->Port = 465;
- $mail->Hostname = ' localhost ';
- $mail->CharSet = ' UTF-8 ';
- $mail->fromname = ' XXXX ';
- $mail->Username =' qq number of the sender ';
- $mail->Password = ' QQ email login password ';
- $mail->from = ' sender's QQ mailbox ';
- $mail->ishtml (true);
- $mail->addaddress (' recipient's QQ email address ', ' QQ nickname ');
- $mail->addaddress (' [email protected] ', ' XXXXX ');
- $mail->Subject = ' This is an example of a phpmailer sending mail ';
- $mail->Body = "This is a <b style=\"color:red;\ ">phpmailer</b> A test case for sending a message ";
- $mail->addattachment ('./src/20151002.png ', ' test.png ');
- $mail->addattachment ('./jlib-1.1.0.js ', ' jlib.js ');
- $Status = $mail->send ();
- if ($status)
- {
- Echo ' Send mail Success '. Date (' y-m-d h:i:s ');;
- }
- Else
- {
- Echo ' failed to send message, error message: '. $mail->ErrorInfo;
- }
- Sleep ($interval);//Hibernate 1minute
- }while (TRUE);
- ?>
The result of the program operation is:
To get rid of the above information,
Keep only "Send success and other information"
Need to be modified in this file: Class.smtp.PHP
Finally, the results are shown as follows:
This enables the automatic sending of messages. Of course, according to the above code, you can also achieve the timing of mail delivery. In this case, there is no more narration.
Reference documents:
Https://github.com/PHPMailer/PHPMailer
http://blog.csdn.NET/whq19890827/article/details/48630061
Http://www.helloweba.com/view-blog-205.html
Http://blog.jjonline.cn/phptech/162.html
http://blog.wpjam.com/m/phpmailer/
Http://blog.51yip.com/php/910.html/comment-page-1
Reference: http://blog.csdn.NET/whq19890827/article/details/48862039
Automatic e-mail delivery for PHP