PHP implements the code for the automatic email sending function (qq mailbox), and php sends emails

Source: Internet
Author: User

PHP implements the code for the automatic email sending function (qq mailbox), and php 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:

<? PHP // send an email to 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 select the pop mode sendmail mode. This article will not explain it. // you can refer to http://phpmailer.github.io/phpmailer/for details in $ mail-> isSMTP (); // smtp authentication required. This must be true $ mail-> SMT H = true; // the address of the server connecting to 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 host domain of the sender to have the localhost content, we recommend that you use your domain name. The default value is localhost $ mail-> Hostname = 'localhost '; // set the encoding of the sent email. Optional values: GB2312 $ mail-> CharSet = 'utf-8'; // set the sender name (nickname) to be any content, reply is not affected (set to qq nickname) $ mail-> FromName = 'xxxx'; // enter the qq number for the smtp Logon account. $ mail-> Username = 'q' for the sender Q '; // enter the smtp logon Password here as "independent Password". If "independent Password" is set, enter the logon qq Password. We recommend that you set "independent Password" $ mail-> Password = 'qq mailbox logon password '; // set the sender's email address. Enter the aforementioned "sender's email address" $ 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. not significant $ mail-> addAddress ('recipient's QQ mail address ', 'qq nicknames '); // you can add multiple recipients // $ mail-> addAddress ('xxxx @ qq.com', 'xxxxx '); // Add the Subject of the email $ mail-> Subject = 'This is a PHPMail Er: Example of sending an email '; // Add the email Body $ mail-> Body = "This Is A <B style = \" color: red; \ "> PHPMailer </B> A test case for sending an email "; // 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 '); // Boolean value returned by the sending command // PS: after testing, if the recipient does not exist. if no error occurs, true is returned. That is, before sending the email, // you need some methods to check whether the email address is valid. $ status = $ mail-> send (); // you can easily determine whether the email address is valid. And the prompt message if ($ status) {echo 'email sent successfully '. date ('Y-m-d H: I: s');} else {echo 'failed to send the 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:

<? Php ignore_user_abort (); // run set_time_limit (0) in the background; // cancel the maximum timeout value of the script running 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 email sending code with the code that runs automatically, You can automatically send Emails:

<? Php // send an email to require '. /mailer/class. phpmailer. php '; require '. /mailer/class. smtp. php '; date_default_timezone_set ('prc'); ignore_user_abort (); // run set_time_limit (0) in the background; // cancel the maximum timeout value of the script running time $ interval = 60*1; // run it 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-> Host Name = 'localhost'; $ mail-> CharSet = 'utf-8'; $ mail-> FromName = 'xxxx'; $ mail-> Username = 'sender's QQ number '; $ 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 <B style = \" color: red; \ "> PHPMailer </B> is a test case for sending emails"; $ mail-> addAttachment ('. /src /20151002.png', 'test.png '); // $ mail-> addAttachment ('. /Jlib-1.1.0.js ', 'jlib. js'); $ status = $ mail-> send (); if ($ status) {echo 'mail sent successfully '. date ('Y-m-d H: I: s');} else {echo 'failed to send the 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.

Summary

The above is the PHP code for automatically sending emails (qq mail) introduced by xiaobian. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.