44邮件使用(视频里的样本)
问题描述
1.发送邮件,如果发送成功,记录一下;
实现描述
1.定义一个send_mail的接口,如果发送成功,需要记录一下为成功;
2.使用接口
dlb_mail.php
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require './vendor/autoload.php';
require_once './vendor/phpmailer/phpmailer/src/Exception.php';
require_once './vendor/phpmailer/phpmailer/src/PHPMailer.php';
require_once './vendor/phpmailer/phpmailer/src/SMTP.php';
//返回数组$toResult的位置:[i]
function getIndexByEmail($toResult,$mail){
foreach($toResult as $k=>$v){
if($v['mail'] == $mail ){
return $k; //找到
}
}
return -1; //未找到
}
//$sendto为发送给谁的邮件地址,类型为string, 比如:'1405533012@qq.com'
//&$toResult,类型为二纬数组,比如: array(0=>array('mail'=>'1405533012@qq.com', 'result'=>0));
//如果sendto发送成功,则更新该地址的'result'为1
function send_mail($sendto, &$toResult){
$mail = new PHPMailer(true);
try {
//Server settings
$mail->ConfirmReadingTo='140****@qq.com';
//
$mail->SMTPDebug = 0; //SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.qq.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = '140********@qq.com'; //SMTP username
$mail->Password = '******'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
//Recipients
$mail->setFrom('1405533012@qq.com', 'Mailer');
$mail->addAddress($sendto);
//$mail->addAddress('qblibo@qq.com', 'qbLibo User'); //Add a recipient
//$mail->addAddress('1405533012@qq.com'); //Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = "dlb subject";
$mail->Body = "dlb body";
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->action_function = static function ($result, $to, $cc, $bcc, $subject, $body) {
global $toResult;
if ($result) {
echo "<br/>";
$index = getIndexByEmail($toResult, $to[0][0]);
if( $index != -1 ){
$toResult[$index]['result']++;
}
echo "<br/>";
} else {
echo "Message send failed\n";
}
};
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
print_r($mail);
}
?>
//send mail test
<?php
require_once "dlb_mail.php";
$toStr='qblibo**********@qq.com,1405533012@qq.com';
$toAry = explode(',',$toStr);
foreach($toAry as $k=>$v)
{
$toResult[$k]['mail'] = $v;
$toResult[$k]['result']=0;
}
foreach($toResult as $k=>$v){
send_mail($v['mail'], $toResult);
echo "call send_mail....<br/>";
sleep(1);
}
print_r($toResult);
?>