PHPMailer的
<?php
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->IsHTML(true);
$mail->SMTPSecure = "tls";
$mail->Mailer = "smtp";
$mail->Host = "smtp.office365.com";
$mail->Port = 587;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "xxx";
$mail->Password = "xxx";
$mail->setFrom('xxx', 'Website');
//Send to Admin
$AdminEmail = 'admin.example@gmail.com';
$mail->AddAddress($AdminEmail, $AdminName);
$mail->Subject = "This is an email";
$mail2 = clone $mail;
$body = 'Hi Admin. This is an email';
$mail->Body = $body;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
//Send to User
$UserEmail = 'user.example@gmail.com';
$mail2->AddAddress($UserEmail, $UserName);
$body2 = 'Hi User. This is an email';
$mail2->Body = $body2;
if(!$mail2->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail2->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
我遇到一个问题,当我发送电子邮件时,管理员会在他们刚收到$mail
时收到$mail2
和$mail
。虽然用户没有问题。通过接收$mail2
正常工作。我试图放$mail->ClearAddresses();
,但它仍然是一样的。一个偶然的机会,问题是什么?
答案 0 :(得分:3)
在添加地址和管理员的内容之前克隆电子邮件变量。 (正如评论中所建议的那样)
答案 1 :(得分:1)
您需要为管理员和用户电子邮件创建不同的对象
//Send to Admin
$mail = new PHPMailer;
$mail->IsHTML(true);
$AdminEmail = 'admin.example@gmail.com';
$mail->AddAddress($AdminEmail, $AdminName);
$mail->Subject = "This is an email";
$mail2 = clone $mail;
$body = 'Hi Admin. This is an email';
$mail->Body = $body;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
//Send to User
$mail = new PHPMailer;
$mail->IsHTML(true);
$UserEmail = 'user.example@gmail.com';
$mail->AddAddress($UserEmail, $UserName);
$body2 = 'Hi User. This is an email';
$mail->Body = $body2;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}