【问题标题】:phpmailer with mysql results带有mysql结果的phpmailer
【发布时间】:2014-02-28 22:08:43
【问题描述】:

我正在尝试使用 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();
    }

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

【问题讨论】:

  • 它发送的所有电子邮件是完全重复的,还是发送多封电子邮件,每个电子邮件的名称和 ID 都不同?
  • 附带说明:mysql 函数已被弃用。请改用 mysqli 或 PDO,因为它们更稳定、更安全!

标签: php mysql email phpmailer


【解决方案1】:

试试这个:

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

【讨论】:

  • 这是我之前尝试过的第一件事,但如果我这样做,只有第一个收件人会收到电子邮件,其他所有收件人都不会收到任何东西...
【解决方案2】:

尝试如下所示,您需要以某种方式计算您的行数,以便它不会重新处理它们。此外,AddAddress();函数用于不断向 TO: 字段添加电子邮件地址,因此您需要调用 ClearAddresses();以便使用一组新的收件人重新开始。

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

    $customers = mysql_query($customers_query);
    $count = mysql_num_rows($customers); 
    $result = mysql_fetch_array($customers);
    $i = 0;

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

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

    while ($i < $count) {
        $email = mysql_result($result,$i,"customer_email");
        $name = mysql_result($result,$i,"customers_name");
        $id = mysql_result($result,$i,"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();
        $i++;
    }

【讨论】:

  • 没有帮助,我仍然收到重复的电子邮件。我尝试删除所有 phpmailer 代码并在循环中添加了一个简单的echo mysql_result($customers,$i,"customer_email") . ' - ' .$i;,我得到了正确的结果(first@domain.com - 1, second@domain.com - 2),但是当我发送电子邮件时,两个收件人都会收到 2 封完全相同的电子邮件。如果我有 3 个结果,则会发送 3 份相同的电子邮件。有什么想法吗?
  • 为你做了一些研究。我编辑了我的答案,现在应该可以解决您的问题了。
  • 应删除重复项,并应向在数据库中找到的每个地址发送一封电子邮件。
  • 不幸的是,这无济于事 - 它发送给第一个收件人,仅此而已,但我添加了错误日志记录并且日志显示未找到收件人地址(对于第一个收件人之后的所有收件人)。感谢您的努力,但我放弃了 - 我会以某种方式手动运行它......
【解决方案3】:

您可以再做一件事,在客户表中添加一个额外的字段,例如 is_email_sent,是或否。发送电子邮件后,您可以使用主键更新特定行。所以它不会多次发送同一封电子邮件..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-23
    • 2020-05-09
    • 1970-01-01
    • 2015-10-24
    • 2018-08-07
    • 2011-09-30
    • 1970-01-01
    相关资源
    最近更新 更多