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
- <? PHP
- // Send email
- Require './mailer/class. phpmailer. php ';
- Require './mailer/class. smtp. php ';
- Date_default_timezone_set ('prc'); // set the mail sending time. If this parameter is not set, the time in other regions is displayed.
- $ Mail = new PHPMailer ();
- // 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.
- $ Mail-> SMTPDebug = 3;
- // Use smtp authentication to send emails. Of course, you can use the pop sendmail method.
- // For more information, see http://phpmailer.github.io/phpmailer /.
- $ Mail-> isSMTP ();
- // Smtp authentication required. This parameter must be set to true.
- $ Mail-> SMTPAuth = true;
- // Link the server address of the qq domain name mailbox
- $ Mail-> Host = 'smtp .qq.com ';
- // 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 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.
- $ Mail-> Hostname = 'localhost ';
- // Set the encoding of the sent email (optional) GB2312
- $ Mail-> CharSet = 'utf-8 ';
- // Set the sender name (nickname) to be any content without affecting the reply (set it to qq nickname)
- $ Mail-> FromName = 'xxxx ';
- // Enter the QQ number for the smtp Logon account.
- $ Mail-> Username = 'sender's QQ No ';
- // 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"
- $ Mail-> Password = 'logon Password of QQ mail ';
- // Set the sender's email address. Enter the "sender's email address" mentioned above"
- $ Mail-> From = 'sender's QQ mailbox ';
- // Whether the email body is sent in html Format
- $ 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 this address. Different email addresses are automatically processed and changed. Here, the second parameter is used. of little significance
- $ Mail-> addAddress ('recipient's QQ address ', 'qq nickname ');
- // You can add multiple recipients.
- // $ Mail-> addAddress ('xxxx @ qq.com ', 'xxxxx ');
- // Add the subject of the email
- $ Mail-> Subject = 'This is an example of PHPMailer sending mail ';
- // Add the email body
- $ Mail-> Body = "this is a test case for sending an email <B style = \" color: red; \ "> PHPMailer </B> ";
- // 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 ('./src/20151002.png', 'test.png ');
- // This method can also be called multiple times to upload multiple attachments
- // $ Mail-> addAttachment ('./Jlib-1.1.0.js', 'jlib. js ');
- // Send the command to return a Boolean Value
- // PS: after testing, if the recipient does not exist. If no error is returned, true is returned, that is, before sending
- // You need some methods to check whether the email address is real and valid.
- $ Status = $ mail-> send ();
- // Simple judgment and prompt information
- If ($ status)
- {
- Echo 'email sent successfully'. date ('Y-m-d H: I: s ');;
- }
- Else
- {
- Echo 'failed to send email, error message not: '. $ mail-> ErrorInfo;
- }
- ?>
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
- <? Php
- Ignore_user_abort (); // run in the background
- Set_time_limit (0); // cancel the upper limit of the script running time
- $ Interval = 60*30; // runs every 30 minutes. The 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 email sending code with the code that runs automatically, You can automatically send Emails:
[Html]View plain copy
- <? Php
- // Send email
- Require './mailer/class. phpmailer. php ';
- Require './mailer/class. smtp. php ';
- Date_default_timezone_set ('prc ');
- Ignore_user_abort (); // run in the background
- Set_time_limit (0); // cancel the upper limit of the script running time
- $ Interval = 60*1; // run 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 = 'sender's QQ No ';
- $ Mail-> Password = 'logon Password of QQ mail ';
- $ Mail-> From = 'sender's QQ mailbox ';
- $ Mail-> isHTML (true );
- $ Mail-> addAddress ('recipient's QQ address ', 'qq nickname ');
- // $ Mail-> addAddress ('xxxx @ qq.com ', 'xxxxx ');
- $ Mail-> Subject = 'This is an example of PHPMailer sending mail ';
- $ Mail-> Body = "this is a test case for sending an email <B style = \" color: red; \ "> PHPMailer </B> ";
- $ Mail-> addAttachment ('./src/20151002.png', 'test.png ');
- // $ Mail-> addAttachment ('./Jlib-1.1.0.js', 'jlib. js ');
- $ Status = $ mail-> send ();
- If ($ status)
- {
- Echo 'email sent successfully'. date ('Y-m-d H: I: s ');;
- }
- Else
- {
- Echo 'failed to send email, error message not: '. $ mail-> ErrorInfo;
- }
- Sleep ($ interval); // sleep 1 minute
- } While (true );
- ?>
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