2015-11-10 38 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