【发布时间】:2016-02-11 07:31:54
【问题描述】:
我正在尝试向不同的用户发送不同的消息。我创建了一个电子邮件地址数组,在遍历它时,我想将 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';
}
}
}
【问题讨论】:
-
你不应该使用
->ClearAllRecipients()吗? -
@tlenss - 我刚刚尝试了大约一分钟,但仍然没有清除收件人...:(
-
确保您使用的是最新版本。查看
clearAllRecipients的代码;它不工作是相当困难的!将addReplyTo和setFrom移到循环之外。查看 PHPMailer 提供的 mailing ist 示例。 -
@Synchro - 伙计,我的 iMac 正在开玩笑。为了测试,我将我的一封电子邮件设置为“user1”,另一封设置为“use2”。默认情况下,Osx Mail 对对话进行分组,虽然发件人地址相同,但它按发件人 (mydomain.eu) 对所有邮件进行分组。我不得不取消选中查看--->“按对话组织”,现在消息只显示在他们自己的邮箱中(他们所属的地方)......:)
标签: php email loops phpmailer email-address