PhpMailer,ClearAddresses()无法正常工作,消息会发送给所有人 [英] PhpMailer, ClearAddresses() won't work, message get sent to everyone

查看:111
本文介绍了PhpMailer,ClearAddresses()无法正常工作,消息会发送给所有人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向不同的用户发送不同的消息.我创建了一个电子邮件地址数组,并在对其进行迭代时,希望将message2发送给user2.

I am trying to send different messages to different users. I made an array of email addresses and while iterating through it, I want to send message2 to user2.

在重用同一邮件实例的同时,在每次迭代的开始,我声明$ mail-> ClearAddresses(),但是现在user2得到了user1和user2的消息,等等.

While reusing the same mail instance, at the beginning of each iteration I declare $mail -> ClearAddresses(), but now user2 gets the message of user1, and user2...and so one.

我想知道在迭代开始时地址不会被清除吗?

What am I missing that the Address won't get cleared at the beginning of the iteration?

谢谢!

//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();

收件人:

$mail->ClearAllRecipients();

这应该可以解决问题.

这篇关于PhpMailer,ClearAddresses()无法正常工作,消息会发送给所有人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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