PHP automatically sends emails, and PHP automatically sends emails.

Source: Internet
Author: User
Tags ssl connection

PHP automatically sends emails, and PHP automatically sends emails.
Recently, I did a mailbox verification function. After studying this function for a while, I completed automatic mail sending. The following uses QQ mail as a demonstration and explains it step by step:

Code

First, the Code is as follows:

[Html]View plain copy
  1. <? PHP
  2. // Send email
  3. Require './mailer/class. phpmailer. php ';
  4. Require './mailer/class. smtp. php ';
  5. Date_default_timezone_set ('prc'); // set the mail sending time. If this parameter is not set, the time in other regions is displayed.
  6. $ Mail = new PHPMailer ();
  7. // Whether to enable smtp debug for debugging the development environment. We recommend that you enable the production environment annotation to disable the debug debugging mode by default.
  8. $ Mail-> SMTPDebug = 3;
  9. // Use smtp authentication to send emails. Of course, you can use the pop sendmail method.
  10. // For more information, see http://phpmailer.github.io/phpmailer /.
  11. $ Mail-> isSMTP ();
  12. // Smtp authentication required. This parameter must be set to true.
  13. $ Mail-> SMTPAuth = true;
  14. // Link the server address of the qq domain name mailbox
  15. $ Mail-> Host = 'smtp .qq.com ';
  16. // Set logon authentication using ssl encryption
  17. $ Mail-> SMTPSecure = 'ssl ';
  18. // Set the remote server port number for ssl connection to the smtp server. Optional values: 465 or 587.
  19. $ Mail-> Port = 465;
  20. // Set the sender's host domain to be optional. The default localhost content is arbitrary. We recommend that you use your domain name. The default localhost is used here.
  21. $ Mail-> Hostname = 'localhost ';
  22. // Set the encoding of the sent email (optional) GB2312
  23. $ Mail-> CharSet = 'utf-8 ';
  24. // Set the sender name (nickname) to be any content without affecting the reply (set it to qq nickname)
  25. $ Mail-> FromName = 'xxxx ';
  26. // Enter the QQ number for the smtp Logon account.
  27. $ Mail-> Username = 'sender's QQ No ';
  28. // Enter the smtp logon password here as "independent password". If you set "independent password", enter the logon qq password. We recommend that you set "independent password"
  29. $ Mail-> Password = 'logon Password of QQ mail ';
  30. // Set the sender's email address. Enter the "sender's email address" mentioned above"
  31. $ Mail-> From = 'sender's QQ mailbox ';
  32. // Whether the email body is sent in html Format
  33. $ Mail-> isHTML (true );
  34. // 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 this address. Different email addresses are automatically processed and changed. Here, the second parameter is used. of little significance
  35. $ Mail-> addAddress ('recipient's QQ address ', 'qq nickname ');
  36. // You can add multiple recipients.
  37. // $ Mail-> addAddress ('xxxx @ qq.com ', 'xxxxx ');
  38. // Add the subject of the email
  39. $ Mail-> Subject = 'This is an example of PHPMailer sending mail ';
  40. // Add the email body
  41. $ Mail-> Body = "this is a test case for sending an email <B style = \" color: red; \ "> PHPMailer </B> ";
  42. // 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)
  43. // The second parameter is the name of the attachment in the email attachment.
  44. $ Mail-> addAttachment ('./src/20151002.png', 'test.png ');
  45. // This method can also be called multiple times to upload multiple attachments
  46. // $ Mail-> addAttachment ('./Jlib-1.1.0.js', 'jlib. js ');
  47. // Send the command to return a Boolean Value
  48. // PS: after testing, if the recipient does not exist. If no error is returned, true is returned, that is, before sending
  49. // You need some methods to check whether the email address is real and valid.
  50. $ Status = $ mail-> send ();
  51. // Simple judgment and prompt information
  52. If ($ status)
  53. {
  54. Echo 'email sent successfully'. date ('Y-m-d H: I: s ');;
  55. }
  56. Else
  57. {
  58. Echo 'failed to send email, error message not: '. $ mail-> ErrorInfo;
  59. }
  60. ?>

In this way, you can send emails. The result is as follows:

To automatically send emails, the program must be able to run automatically. The code for automatically running the program in the background is as follows:

[Html]View plain copy
  1. <? Php
  2. Ignore_user_abort (); // run in the background
  3. Set_time_limit (0); // cancel the upper limit of the script running time
  4. $ Interval = 60*30; // runs every 30 minutes. The 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 email sending code with the code that runs automatically, You can automatically send Emails:

 

 

[Html]View plain copy
  1. <? Php
  2. // Send email
  3. Require './mailer/class. phpmailer. php ';
  4. Require './mailer/class. smtp. php ';
  5. Date_default_timezone_set ('prc ');
  6. Ignore_user_abort (); // run in the background
  7. Set_time_limit (0); // cancel the upper limit of the script running time
  8. $ Interval = 60*1; // run 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 = 'sender's QQ No ';
  21. $ Mail-> Password = 'logon Password of QQ mail ';
  22. $ Mail-> From = 'sender's QQ mailbox ';
  23. $ Mail-> isHTML (true );
  24. $ Mail-> addAddress ('recipient's QQ address ', 'qq nickname ');
  25. // $ Mail-> addAddress ('xxxx @ qq.com ', 'xxxxx ');
  26. $ Mail-> Subject = 'This is an example of PHPMailer sending mail ';
  27. $ Mail-> Body = "this is a test case for sending an email <B style = \" color: red; \ "> PHPMailer </B> ";
  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 'email sent successfully'. date ('Y-m-d H: I: s ');;
  34. }
  35. Else
  36. {
  37. Echo 'failed to send email, error message not: '. $ mail-> ErrorInfo;
  38. }
  39. Sleep ($ interval); // sleep 1 minute
  40. } While (true );
  41. ?>


 

The program running result is:

 

To remove the above information,

Only "sent successfully and other information" is retained"

In this file, modify class. smtp. PHP.

 

The result is as follows:

In this way, the mail is automatically sent. Of course, according to the above code, you can also regularly send emails. This is not described here.

References:

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

 

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.