gpt4 book ai didi

php - phpmailer中的for循环

转载 作者:行者123 更新时间:2023-12-04 05:18:00 24 4
gpt4 key购买 nike

我正在尝试将电子邮件发送给 10 个不同的用户,我已将变量 $friendsEmails 放入一个包含 10 个不同电子邮件的数组中,但是看起来它会为每封 10x10 的电子邮件复制 10 个。我做错了什么吗?

  for($i =0; $i<11; $i++){

$mail->SetFrom($email, $name);

$mail->AddReplyTo($email,$name);

$mail->Subject = "We wish you a merry Christmas";

$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$mail->AddAddress($friendsEmails[$i], $friendsNames[$i]);

if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

}

最佳答案

难怪您要发送多封电子邮件,因为在 for 循环的每次迭代中,您只是在添加新地址。使用 PHPMailer::clearAllRecipients()在添加新电子邮件地址之前从上一次迭代中删除数据。

for($i =0; $i<11; $i++){
$mail->SetFrom($email, $name);
$mail->AddReplyTo($email,$name);
$mail->Subject = "We wish you a merry Christmas";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress($friendsEmails[$i], $friendsNames[$i]);

if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
$mail->clearAllRecipients(); // Clear all recipient types(to, bcc, cc).
}

关于php - phpmailer中的for循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13985394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com