44郵件使用(視頻里的樣本)
問(wèn)題描述
1.發(fā)送郵件,如果發(fā)送成功,記錄一下;
實(shí)現(xiàn)描述
1.定義一個(gè)send_mail的接口,如果發(fā)送成功,需要記錄一下為成功;
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';
//返回?cái)?shù)組$toResult的位置:[i]
function getIndexByEmail($toResult,$mail){
? ?foreach($toResult as $k=>$v){
? ? ? ?if($v['mail'] == $mail ){
? ? ? ? ? ?return $k; //找到
? ? ? ?}
? ?}
? ?return -1; //未找到
}
//$sendto為發(fā)送給誰(shuí)的郵件地址,類(lèi)型為string, 比如:'1405533012@qq.com'
//&$toResult,類(lèi)型為二緯數(shù)組,比如: array(0=>array('mail'=>'1405533012@qq.com', 'result'=>0));
//如果sendto發(fā)送成功,則更新該地址的'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);
?>