phpmailer循环中重复的电子邮件地址(和可能的消息) [英] phpmailer loop duplicated email addresses (and possible message) in loop

查看:22
本文介绍了phpmailer循环中重复的电子邮件地址(和可能的消息)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following code sending email shipping confirmations to my customers:

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循环中,每个新客户地址都被添加到列表中,而没有清除它.尽管消息似乎没有重复,但似乎没有发生什么?循环中的每一遍都不会重复吗?

It appears to be sending to multiple emails. What I believe is happening is that in the while loop each new customers address is added to the list without clearing it. What doesnt seem to be happening though is the message does not seem to be duplicated? Wouldn't that duplicate at as well with each pass in the loop?

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

In any case I think the best thing to do before the try is:

$mail = new PHPMailer(true);

,这样,每次while循环通过时,$ mail对象将再次实例化.那合适吗?我知道存在clearAllRecipients()函数,但我还想确保身体也被清洁.

so that with each pass of the while loop the $mail object would be instantiated again. Is that proper? I know that the clearAllRecipients() function exists but i also want to be sure the body is cleaned as well.

推荐答案

这是因为 addAddress()确实按照其名称的含义进行了操作;它会添加一个将邮件发送到的地址.它没有设置将邮件发送到的地址.

It's because addAddress() does exactly what its name suggests; it adds an address that a message will be sent to. It doesn't set the addresses a message will be sent to.

不足为奇的是,有一种方法允许您清除地址列表,称为 clearAddresses(),因此只需在 send()之后在发送循环的末尾调用它即可.code>,您的问题将得到解决.

Unsurprisingly there is a method that allows you to clear the list of addresses, called clearAddresses(), so just call that at the end of your sending loop after send() and your issue will be solved.

我还建议您将代码基于邮件清单示例与PHPMailer一起提供,因为它将比您这里的代码快得多.另请阅读有关发送到列表的文档 https://github.com/PHPMailer/PHPMailer/wiki/发送到列表)以获取更多建议.

I'd also recommend that you base your code on the mailing list example provided with PHPMailer as it will be much faster than the code you have here. Also read the docs on sending to listshttps://github.com/PHPMailer/PHPMailer/wiki/Sending-to-lists) for furtehr advice.

这篇关于phpmailer循环中重复的电子邮件地址(和可能的消息)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆