我一直在尝试许多不同的方法来阻止PHP Mailer将每个来自我的数据库的电子邮件地址附加到每封电子邮件中。
我觉得每个用户从我的网站收到一封电子邮件并且能够看到同样的电子邮件中附加的其他几千封电子邮件也不合适。
这是我的代码,我已经尝试了很多不同的东西:
$query_select = "SELECT * FROM users";
$query = mysqli_query($connection, $query_select);
if($select_to == 'all'){
while($row = mysqli_fetch_assoc($query)) {
$user_email = $row['user_email'];
$send_to = $user_email;
$mail->setFrom('example@gmail.com', 'My website');
$mail->addAddress($send_to);
$mail->isHTML(true);
$mail->Subject = $subject_email;
$mail->Body = $content_email;
$mail->AltBody = $content_email;
}
} else {
if($select_to == 'single'){
$send_to = $to_single_email;
}
}
if($mail->send()){
$confirm = 'Sent.';
$send = array(
'confirm'=>$confirm
);
echo json_encode($send);
} else {
$confirm = 'There was en error while sending this email.';
$send = array(
'confirm'=>$confirm
);
echo json_encode($send);
}
答案 0 :(得分:3)
$mail->send()
将通过单条消息发送到所有地址。相反,您需要为每个收件人发送一条消息。
while()
循环将被重构,类似于:
while($row = mysqli_fetch_assoc($query)) {
$user_email = $row['user_email'];
$send_to = $user_email;
$mail->setFrom('example@gmail.com', 'My website');
$mail->addAddress($send_to);
$mail->isHTML(true);
$mail->Subject = $subject_email;
$mail->Body = $content_email;
$mail->AltBody = $content_email;
$mail->send();
$mail->clearAllRecipients();
}
对clearAllRecipients()
的调用将清除之前通过addAddress()
累积的地址
您还需要在每次调用send()