我有这段代码可以使用PHPMailer发送邮件。问题在于,使用我当前的代码重复发送邮件。它以这种格式显示。
邮件发送至:
user@gmail.com (1st go)
user1@gmail.com, user@gmail.com (2nd go)
user2@gmail.com, user1@gmail.com, user@gmail.com (3rd go)
user3@gmail.com, user2@gmail.com, user1@gmail.com, user@gmail.com (4th go)
....依此类推。
我认为这是因为我的while循环逻辑。将批量电子邮件发送一次到数据库中的成员而又无需重复的另一种方式是什么?
这是我的代码:
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Host = $mainf['set_smtp_host'];
$mail->Port = $mainf['set_smtp_port'];
$mail->SMTPSecure = $mainf['set_smtp_security'];
$mail->IsHTML(true);
$mail->SMTPAuth = true;
$mail->Username = $mainf['set_smtp_uname'];
$mail->Password = $mainf['set_smtp_pass'];
$mail->setFrom($mainf['set_noreply_email'], $mainf['set_site_name']);
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->Subject = $sub;
$mail->Body = $mail_msg;
$emails = $pdo->prepare("SELECT mem_name, mem_email FROM members WHERE mem_email_verified = 'yes' ORDER BY mem_id ASC LIMIT 5");
$emails-> execute();
while($u = $emails->fetch()){
$mail->addAddress($u['mem_email'], $u['mem_name']);
$send = $mail->Send();
}
if($send){
$msg = "<div class='alert alert-success'>Mail sent to all members successfully.</div>";
}else{
$msg = "<div class='alert alert-danger'>Mail Server Error! Please refresh the page and try again.</div>";
}
此外,在邮箱中,我可以看到将邮件发送给的其他人。我可以添加“密件抄送”选项仅发送一次大容量电子邮件,而不会重复发送给任何人吗?
答案 0 :(得分:3)
// option 1
while($u = $emails->fetch()){
$mail->addAddress($u['mem_email'], $u['mem_name']);
$send = $mail->Send();
$mail->ClearAllRecipients(); // reset the `To:` list to empty
}
// option 2
while($u = $emails->fetch()){
$mail->addAddress($u['mem_email'], $u['mem_name']);
$mail->AddBCC($u[0]);
}
$mail->send();