我正在尝试向不同的用户发送不同的消息。我制作了一系列电子邮件地址,并在迭代它时,我想将message2发送给user2。
在重复使用相同的邮件实例时,在每次迭代开始时我声明$ mail - > ClearAddresses(),但现在user2获取user1和user2的消息......等等。
我错过了什么地址在迭代开始时不会被清除?
谢谢!
//settings
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'xxx'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxx'; // SMTP username
$mail->Password = 'xxx'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;
$mail->CharSet = "UTF-8"; // TCP port to connect to
function sendInvoice($mail, $addresses){
foreach($addresses as $recipient){
$mail->ClearAddresses();
$mail->setFrom('mail@domain.eu', 'My Server');
$mail->addAddress($recipient['email'], $recipient['name']); // Add a recipient
$mail->addReplyTo('mail@domain.eu', 'My Server');
$mail->isHTML(true);
$mail->Subject = $recipient[subject];
//$mail->Body = $message;
$mail->MsgHTML($recipient[message]);
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
//echo 'Message has been sent';
}
}
}
答案 0 :(得分:3)
在您的代码中,更改:
$mail->ClearAddresses();
为:
$mail->ClearAllRecipients();
这应解决问题。