phpmailer 与 mysql 结果

标签 php mysql email phpmailer

我正在尝试使用 phpmailer 向数据库中找到的每个电子邮件地址发送电子邮件,但作为唯一的电子邮件。由于某种原因,它发送重复的电子邮件,并且它会以与我的查询返回的行一样多的副本发送出去。因此,如果我的查询返回 5 行,则每个收件人将收到 5 封电子邮件(发送的电子邮件总数为 25 封)。我无法对多个收件人使用同一电子邮件,因为电子邮件内容是个性化的。

我的代码做错了什么?请帮忙...

这是我的代码:

$customers_query = "SELECT customer_name, customer_email, customer_id FROM customers";

    $customers = mysql_query($customers_query);

    if (!$customers) {
        $message  = 'Error notice: ' . mysql_error() . "\n";
        $message .= 'Whole query: ' . $query;
    die($message);
    }

    require_once('class.phpmailer.php');
    // create an instance

    while ($row = mysql_fetch_array($customers)) {
        $email = $row['customer_email'];
        $name = $row['customers_name'];
        $id = $row['customer_id'];
        $mail = new PHPMailer();
        // use sendmail for the mailer
        $mail->IsSendmail();
        $mail->SingleTo = true;
        $mail->IsHTML(true);
        $mail->From = "noreply@domain.com";
        $mail->FromName = "MyWebsite";
        $mail->Subject = "Welcome to MyWebsite";
        $mail->AddAddress($email);

        $mail->Body = "Dear ".$name.", welcome to MyWebsite. Your ID is: ".$id.". Enjoy your stay.";
        $mail->Send();
    }

那么,这里缺少什么?为什么它会发送那么多电子邮件?

最佳答案

试试这个:

$mail->ClearAddresses(); after $mail->Send();

关于phpmailer 与 mysql 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22106774/

相关文章:

JavaScript 将小数转换为整数

php - 简单的 afterSave 方法在 Yii 中不起作用

php - 如何将 MySQLi 转换为准备好的语句?

php - 检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行 '07:28:29)' 附近使用的正确语法

.net - 如何使用System.Net.Mail设置退回邮件地址?

email - 指定电子邮件的退回地址

php - ZF2 - 从路线生成网址

php - Laravel php 规范列表表

mysql - 如何将一个表中的两个字段连接到第二个表中?

ruby-on-rails - 如何使用/解决Gmail的SMTP出站发送限制?