【问题标题】:phpmailer loop duplicated email addresses (and possible message) in loopphpmailer 在循环中循环重复的电子邮件地址(和可能的消息)
【发布时间】:2020-01-06 19:49:11
【问题描述】:

我有以下代码向我的客户发送电子邮件发货确认:

while ($i < count($tracked) ) {
try {
        //Server settings
        //$mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
        $mail->isSMTP();                                            // Send using SMTP
        $mail->Host       = 'smtp.office365.com';                    // Set the SMTP server to send through
        $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
        $mail->Username   = 'support@mycompany.com';                     // SMTP username
        $mail->Password   = 'password';                               // SMTP password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
        $mail->Port       = 587;                                    // TCP port to connect to

        //Recipients
        $mail->setFrom('support@mycompany.com', 'mycompany');
        $mail->addAddress($tracked[$i]['customers_email_address'], $tracked[$i]['customers_name']);     // Add a recipient
        $mail->addReplyTo('support@mycompany.com', 'mycompany');


        // Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = EMAIL_TEXT_SUBJECT;
        $mail->Body    = $html;
$mail->AltBody = 'Your order with mycompany.com has shipped';
        $mail->send();
        echo 'order confirmation sent to order#:'.$tracked[$i]['orders_id'].'<br/>';
    } catch (Exception $e) {
        echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }
    $i++;
}

它似乎正在发送到多个电子邮件。我相信正在发生的事情是,在 while 循环中,每个新客户地址都被添加到列表中而不清除它。尽管消息似乎没有重复,但似乎没有发生什么?循环中的每次传递不会重复吗?

无论如何,我认为在尝试之前最好的做法是:

$mail = new PHPMailer(true);

这样每次循环的时候 $mail 对象都会被再次实例化。这合适吗?我知道 clearAllRecipients() 函数存在,但我也想确保身体也被清洁。

【问题讨论】:

  • 按照answer below 中的建议处理您的地址问题,但我还建议您将除实际发送邮件($mail-&gt;send(); 和回显)之外的所有内容移到while 循环。没有理由在循环内做所有其他事情。做一次,然后重复使用。

标签: php loops phpmailer


【解决方案1】:

看起来你在循环一个 $tracked 数组。为什么不使用 foreach 循环?那么你就不需要使用计数器了。

    foreach ($tracked as $track ) {
try {
        //Server settings
        //$mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
        $mail->isSMTP();                                            // Send using SMTP
        $mail->Host       = 'smtp.office365.com';                    // Set the SMTP server to send through
        $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
        $mail->Username   = 'support@mycompany.com';                     // SMTP username
        $mail->Password   = 'password';                               // SMTP password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
        $mail->Port       = 587;                                    // TCP port to connect to

        //Recipients
        $mail->setFrom('support@mycompany.com', 'mycompany');
        $mail->addAddress($track['customers_email_address'], $track['customers_name']);     // Add a recipient
        $mail->addReplyTo('support@mycompany.com', 'mycompany');


        // Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = EMAIL_TEXT_SUBJECT;
        $mail->Body    = $html;
$mail->AltBody = 'Your order with mycompany.com has shipped';
        $mail->send();
        echo 'order confirmation sent to order#:'.$track['orders_id'].'<br/>';
    } catch (Exception $e) {
        echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }
}

【讨论】:

  • 这会稍微清理一下(只是去掉计数器),但它仍然会遇到相同的问题,即 setFrom 不断为每个循环添加一个新的电子邮件地址?
【解决方案2】:

这是因为addAddress() 完全符合其名称的含义;它添加消息将发送到的地址。它不会设置消息将发送到的地址。

不出所料,有一种方法可以让您清除地址列表,称为clearAddresses(),因此只需在发送循环结束时在send() 之后调用它,您的问题就会得到解决。

我还建议您将代码基于the mailing list example provided with PHPMailer,因为它比您在此处的代码要快得多。另请阅读有关发送至 listshttps://github.com/PHPMailer/PHPMailer/wiki/Sending-to-lists) 的文档以获取更多建议。

【讨论】:

  • 有道理,谢谢。我已将 clearAddresses 添加到每个循环的末尾,并将对其进行调整,以便我们不会设置不会一遍又一遍地更改的相同项目。不过,有一个问题,每封电子邮件的正文都发生了变化(例如,跟踪号和订单号略有变化)。设置body会清除之前的body吗?
  • 是的; body 只是一个普通的属性。
猜你喜欢
  • 2023-01-20
  • 1970-01-01
  • 1970-01-01
  • 2020-02-07
  • 1970-01-01
  • 1970-01-01
  • 2020-02-24
  • 2014-07-29
  • 2012-08-09
相关资源
最近更新 更多