2015-11-10 39 views
2

我试图向不同的用户发送不同的邮件。我制作了一系列电子邮件地址,并在遍历它时想发送message2给user2。PhpMailer,ClearAddresses()将不起作用,邮件会发送给所有人

重复使用相同的邮件实例时,在每次迭代开始时我声明$ 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('[email protected]', 'My Server'); 
      $mail->addAddress($recipient['email'], $recipient['name']);   // Add a recipient 
      $mail->addReplyTo('[email protected]', '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'; 
       } 
     } 

    } 
+1

你不应该使用' - > ClearAllRecipients()'吗? – tlenss

+0

可能重复的[phpMailer - 你如何删除收件人](http://stackoverflow.com/questions/10952441/phpmailer-how-do-you-remove-recipients) – tlenss

+0

@tlenss - 我刚刚尝试了一分钟,并且仍然不清除收件人... :( –

回答

2

在你的代码,更改:

$mail->ClearAddresses(); 

到:

$mail->ClearAllRecipients(); 

这应该可以解决这个问题。

+0

该方法启动用大写字母,请不要恢复为小写字母。 – itoctopus