Automatic e-mail delivery for PHP

Source: Internet
Author: User
Tags server port ssl connection

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
  1. <? PHP
  2. Send mail
  3. Require './mailer/class.phpmailer.php ';
  4. Require './mailer/class.smtp.php ';
  5. Date_default_timezone_set (' PRC ');//Set the time for the message to be sent and, if not set, the time of the other zone
  6. $mail = new Phpmailer ();
  7. 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
  8. $mail->smtpdebug = 3;
  9. 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
  10. Refer to the http://phpmailer.github.io/PHPMailer/for more information
  11. $mail->issmtp ();
  12. SMTP needs authentication This must be true
  13. $mail->smtpauth=true;
  14. The server address of the link QQ domain mailbox
  15. $mail->Host = ' smtp.qq.com ';
  16. Set login authentication using SSL encryption
  17. $mail->smtpsecure = ' SSL ';
  18. To set the remote server port number for an SSL connection to an SMTP server optional 465 or 587
  19. $mail->Port = 465;
  20. Set Sender's host domain optional default to localhost content arbitrary, recommended to use your domain name, here is the default localhost
  21. $mail->Hostname = ' localhost ';
  22. To set the encoding of sent messages optional GB2312
  23. $mail->CharSet = ' UTF-8 ';
  24. Set sender name (nickname) can be any content, do not affect the reply (set as QQ nickname can be)
  25. $mail->fromname = ' XXXX ';
  26. SMTP login account here to fill in QQ number can be
  27. $mail->Username =' qq number of the sender ';
  28. 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"
  29. $mail->Password = ' QQ email login password ';
  30. Set the sender's e-mail address here fill in the "sender's mailbox" mentioned above
  31. $mail->from = ' sender's QQ mailbox ';
  32. Whether the message body is sent in HTML
  33. $mail->ishtml (true);
  34. 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.
  35. $mail->addaddress (' recipient's QQ email address ', ' QQ nickname ');
  36. Multiple recipients can be added
  37. $mail->addaddress (' [email protected] ', ' XXXXX ');
  38. Add the subject of the message
  39. $mail->Subject = ' This is an example of a phpmailer sending mail ';
  40. Add Message body
  41. $mail->Body = "This is a <b style=\"color:red;\ ">phpmailer</b>  A test case for sending a message ";
  42. 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)
  43. The second parameter is the name of the attachment in the message attachment
  44. $mail->addattachment ('./src/20151002.png ', ' test.png ');
  45. The same method can be called multiple times to upload several attachments
  46. $mail->addattachment ('./jlib-1.1.0.js ', ' jlib.js ');
  47. Send command returns a Boolean value
  48. PS: Tested, if the recipient does not exist. Returns true if no error occurs, which means that before sending
  49. I need some method to detect whether the mailbox is real and effective
  50. $Status = $mail->send ();
  51. Simple judgment and prompt information
  52. if ($status)
  53. {
  54. Echo ' Send mail Success '. Date (' y-m-d h:i:s ');;
  55. }
  56. Else
  57. {
  58. Echo ' failed to send message, error message: '. $mail->ErrorInfo;
  59. }
  60. ?>

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
    1. <? PHP
    2. Ignore_user_abort (); Running in the background
    3. Set_time_limit (0); Maximum timeout for canceling script run time
    4. $interval=60*30;//run every half hour, this interval can be modified as needed
    5. do{
    6. XXXXX//Here is the code you want to execute
    7. Sleep ($interval); Sleep for half an hour
    8. }while (TRUE);
    9. ?>

By combining the code that sends the message with the code that runs automatically, you can automate the message delivery:

[HTML]View PlainCopy
  1. <? PHP
  2. Send mail
  3. Require './mailer/class.phpmailer.php ';
  4. Require './mailer/class.smtp.php ';
  5. Date_default_timezone_set (' PRC ');
  6. Ignore_user_abort ();//Background run
  7. Set_time_limit (0);//Cancel the timeout limit for script run time
  8. $interval = 60*1;//run once every minute (this can be adjusted as needed)
  9. do{
  10. $mail = new Phpmailer ();
  11. $mail->smtpdebug = 3;
  12. $mail->issmtp ();
  13. $mail->smtpauth=true;
  14. $mail->Host = ' smtp.qq.com ';
  15. $mail->smtpsecure = ' SSL ';
  16. $mail->Port = 465;
  17. $mail->Hostname = ' localhost ';
  18. $mail->CharSet = ' UTF-8 ';
  19. $mail->fromname = ' XXXX ';
  20. $mail->Username =' qq number of the sender ';
  21. $mail->Password = ' QQ email login password ';
  22. $mail->from = ' sender's QQ mailbox ';
  23. $mail->ishtml (true);
  24. $mail->addaddress (' recipient's QQ email address ', ' QQ nickname ');
  25. $mail->addaddress (' [email protected] ', ' XXXXX ');
  26. $mail->Subject = ' This is an example of a phpmailer sending mail ';
  27. $mail->Body = "This is a <b style=\"color:red;\ ">phpmailer</b>  A test case for sending a message ";
  28. $mail->addattachment ('./src/20151002.png ', ' test.png ');
  29. $mail->addattachment ('./jlib-1.1.0.js ', ' jlib.js ');
  30. $Status = $mail->send ();
  31. if ($status)
  32. {
  33. Echo ' Send mail Success '. Date (' y-m-d h:i:s ');;
  34. }
  35. Else
  36. {
  37. Echo ' failed to send message, error message: '. $mail->ErrorInfo;
  38. }
  39. Sleep ($interval);//Hibernate 1minute
  40. }while (TRUE);
  41. ?>


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

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.