首页 > 解决方案 > 如何使用 php mailer 仅在 TO 地址上显示相关电子邮件

问题描述

使用 phpmailer 发送电子邮件时如何隐藏其他电子邮件?我收到来自数据库的电子邮件。该代码正在向所有人发送邮件,但在其显示所有电子邮件的标题上。请帮助我的代码如下:

$stmt = $conn->prepare("SELECT * FROM subscribers");
$stmt->execute();
$results= $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($results as $row){
    $mail->addAddress($row['email']); 
}

标签: phpphpmailer

解决方案


您可以在循环中添加发送函数,但是这在大型数据集上是资源昂贵的。

$stmt = $conn->prepare("SELECT * FROM subscribers");
$stmt->execute();
$results= $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($results as $row){
    $mail->addAddress($row['email']); 
    $mail->Send();
    $mail->clearAddresses();
}

您也可以将此作为参考,以更有效地执行此操作

https://github.com/PHPMailer/PHPMailer/blob/master/examples/mailing_list.phps

正如KIKO Software上面评论中提到的,如果电子邮件不是针对每个用户进行个性化的,那么您可以$mail->addBcc($row['email'])在循环内使用并批量发送所有电子邮件。

// add a main email address
$mail->addAddress('an_email_here');
foreach($results as $row){
    // then just bcc other emails.
    $mail->addBcc($row['email'])
}
$mail->Send()

注意:这也会向用户显示电子邮件刚刚BCC编辑


推荐阅读