This article will share with you, taking qq mailbox as an example to introduce PHP function code to automatically send emails. Friends who are interested can refer to it
I recently made an email verification function, studied it for a while, and got it. Automatically send emails. The following uses qq mailbox as a demonstration to explain step by step:
Code download address
First of all, it is to send the email, the code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | <?PHP
require './mailer/ class .phpmailer.php';
require './mailer/ class .smtp.php';
date_default_timezone_set('PRC');
$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 ='发送者的QQ号';
$mail ->Password = 'QQ邮箱的登录密码';
$mail ->From = '发送者的QQ邮箱';
$mail ->isHTML(true);
$mail ->addAddress('收件人的QQ邮箱地址','QQ昵称');
$mail ->Subject = '这是一个PHPMailer发送邮件的示例';
$mail ->Body = "这是一个<b style=\"color:red;\">PHPMailer</b>发送邮件的一个测试用例" ;
$mail ->addAttachment('./src/20151002.png','test.png');
$status = $mail ->send();
if ( $status )
{
echo '发送邮件成功'. date ('Y-m-d H:i:s');;
}
else
{
echo '发送邮件失败,错误信息未:'. $mail ->ErrorInfo;
}
?>
|
Copy after login
#This way you can send emails. The result is as follows:

If you want to automatically send emails, then the program must be able to run automatically. The code for the program to automatically run in the background is as follows:
1 2 3 4 5 6 7 8 9 | <?php
ignore_user_abort();
set_time_limit(0);
$interval =60*30;
do {
XXXXX
sleep( $interval );
} while (true);
?>
|
Copy after login
Combine the code for sending emails and the code for automatic running. You can realize automatic sending of emails:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?php
require './mailer/ class .phpmailer.php';
require './mailer/ class .smtp.php';
date_default_timezone_set('PRC');
ignore_user_abort();
set_time_limit(0);
$interval = 60*1;
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 ='发送者的QQ号';
$mail ->Password = 'QQ邮箱的登录密码';
$mail ->From = '发送者的QQ邮箱';
$mail ->isHTML(true);
$mail ->addAddress('收件人的QQ邮箱地址','QQ昵称');
$mail ->Subject = '这是一个PHPMailer发送邮件的示例';
$mail ->Body = "这是一个<b style=\"color:red;\">PHPMailer</b>发送邮件的一个测试用例" ;
$mail ->addAttachment('./src/20151002.png','test.png');
$status = $mail ->send();
if ( $status )
{
echo '发送邮件成功'. date ('Y-m-d H:i:s');;
}
else
{
echo '发送邮件失败,错误信息未:'. $mail ->ErrorInfo;
}
sleep( $interval );
} while (true);
?>
|
Copy after login
The program running result is:

To remove the above information,
only keep the "send successfully and other information"
Need to modify this file: class.smtp.PHP

The final result is displayed as follows:

In this way, the automatic sending of emails is realized. Of course, according to the above code, scheduled emails can also be sent.
Related recommendations:
php emailautomatically sent
pure javascript implementationautomatic Send Email
PHP 163 Email Automatically Send
The above is the detailed content of PHP realizes automatic sending mail function code (qq mailbox)_php example. For more information, please follow other related articles on the PHP Chinese website!