PhpMailer,ClearAddresses()赢得';不工作,信息发送给每个人

PhpMailer,ClearAddresses()赢得';不工作,信息发送给每个人,php,email,loops,phpmailer,email-address,Php,Email,Loops,Phpmailer,Email Address,我正在尝试向不同的用户发送不同的消息。我创建了一个电子邮件地址数组,在迭代过程中,我想将message2发送给user2 在重用同一个邮件实例时,在每次迭代开始时,我声明$mail->ClearAddresses(),但现在user2获得了user1和user2的消息,依此类推 我遗漏了什么,在迭代开始时地址不会被清除 谢谢 //settings $mail = new PHPMailer; $mail->isSMTP();

我正在尝试向不同的用户发送不同的消息。我创建了一个电子邮件地址数组,在迭代过程中,我想将message2发送给user2

在重用同一个邮件实例时,在每次迭代开始时,我声明$mail->ClearAddresses(),但现在user2获得了user1和user2的消息,依此类推

我遗漏了什么,在迭代开始时地址不会被清除

谢谢

//settings

        $mail = new PHPMailer;

        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'xxx';                  // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = 'xxx';          // SMTP username
        $mail->Password = 'xxx';                    // SMTP password
        $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 465;  
        $mail->CharSet = "UTF-8";                             // TCP port to connect to

    function sendInvoice($mail, $addresses){

        foreach($addresses as $recipient){

            $mail->ClearAddresses();
            $mail->setFrom('mail@domain.eu', 'My Server');
            $mail->addAddress($recipient['email'], $recipient['name']);         // Add a recipient
            $mail->addReplyTo('mail@domain.eu', 'My Server');


            $mail->isHTML(true);

            $mail->Subject = $recipient[subject];
            //$mail->Body    = $message;
            $mail->MsgHTML($recipient[message]);        

                if(!$mail->send()) {

                    echo 'Message could not be sent.';
                    echo 'Mailer Error: ' . $mail->ErrorInfo;

                } else {                    
                    //echo 'Message has been sent';
                }
        }

    }
在代码中,更改:

$mail->ClearAddresses();
致:


这应该可以解决问题。

您是否应该使用
->ClearAllRecipients()
?可能重复@tlenss?我刚刚尝试了大约一分钟,但仍然没有清除收件人…:(确保您使用的是最新版本。查看
clearAllRecipients
的代码;它很难不工作!将
addReplyTo
setFrom
移动到循环之外。查看phpMail提供的邮件列表示例。@Synchro-Man,我的iMac正在开玩笑。为了测试,我将我的一封电子邮件设置为“user1”另一个名称为“use2”。Osx Mail默认情况下会对会话进行分组,虽然发件人地址相同,但它会按发件人(mydomain.eu)对所有邮件进行分组。我必须取消选中查看-->“按会话组织”,现在邮件仅显示在其自己的邮箱中(它们所属的邮箱中)该方法以大写开头,请不要还原为小写。
$mail->ClearAllRecipients();