Automatic sending of PHP mail

Source: Internet
Author: User
Tags ssl connection

<?  PHP//Mail send require './mailer/class.phpmailer.php ';  Require './mailer/class.smtp.php ';     Date_default_timezone_set (' PRC ');//Set the time for the message to be sent, if not set, the time of the other zone is displayed $mail = new Phpmailer ();    Whether to enable debug for SMTP debugging development environment It is recommended to turn on the production environment comments off can be turned off by default Debug debug mode $mail->smtpdebug = 3; Use SMTP authentication method to send mail, of course you can choose Pop mode SendMail way and so on not to do the detailed//can refer to http://phpmailer.github.io/PHPMailer/in the details of the introduction $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 the login authentication using SSL encryption $mail->smtpsecure = ' SSL ';    Set the remote server port number for the SSL connection to the 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 ';    Set the encoding of the sent message GB2312 $mail->charset = ' UTF-8 ';    Set the sender's name (nickname) can be any content, does not affect the reply (set as QQ nickname can) $mail->fromname = ' XXXX ';    SMTP login account here to fill in QQ number can $mail->username = ' sender's QQ number ';    The password for the SMTP login is filled in with "Independent password", if the password is set "Independent password" is entered into QQ passwords recommended setting "Independent password" $mail->password = ' QQ mailbox login password '; Set the sender's e-mail address.Fill in the above mentioned "sender's mailbox" $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 for that address. The second parameter is not very meaningful here $mail->addaddress (' recipient's QQ email address ', '    QQ nickname ');    Multiple recipients//$mail->addaddress (' XXXX@qq.com ', ' XXXXX ') can be added;    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 mail"; 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 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 ');  The Send command returns a Boolean value of//ps: tested, if the recipient does not exist. Returns true if the error does not occur, that is, before sending//oneself need some method to detect whether the mailbox is real and valid $status = $mail->send ();  Simple judgment with cue message if ($status) {echo ' Send message succeeded '. 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:

<?php  Ignore_user_abort ();//Background run  set_time_limit (0);//Cancel the timeout limit for 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 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:

<?php//Mail send 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 time-out limit of the script runtime $interval = 60*1;//Run once every minute (this can be adjusted as needed) do{$mail = n   EW 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 = ' The sender's QQ mailbox ';     $mail->ishtml (TRUE);    $mail->addaddress (' recipient's QQ email address ', ' QQ nickname ');    $mail->addaddress (' XXXX@qq.com ', ' 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 mail";  $mail->addattachment ('./src/20151002.png ', ' test.png ');  $mail->addattachment ('./jlib-1.1.0.js ', ' jlib.js '); $stATUs = $mail->send ();  if ($status) {echo ' sent the message successfully '. 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.

Related Article

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.