2017-08-01 107 views
0

我有一個foreach循環,我從中爲我的客戶發送電子郵件。 我有我的分貝1,2,3,4 所以根據foreach循環循環將運行4次發送電子郵件給每個客戶 但在第一次運行它發送電子郵件給客戶1. 在第2次運行它發送電子郵件給客戶1和2. 在第三次運行中,它發送電子郵件給客戶1,2,3等。ForEach循環值保持以前的值

我該如何向一個客戶發送一封電子郵件。 這是我的代碼。

$list=$tmp_con->query("SELECT name,email FROM users WHERE user_type='$tp' AND subscribed='yes' AND verified='1'") or die($tmp_con->error); 
    $lis=array(); 

while($row=$list->fetch_array()){ 
    $lis[] = $row; 
} 
foreach($lis as $lst){ 


     $content = str_replace("{name}", $lst['name'], $mail_content); 

     $content=$content."".$append_unsub; 

     /**************************************************phpmailer class***********************/ 

       //$mail->SMTPDebug = 3;        // Enable verbose debug output 

       $mail->isSMTP();          // Set mailer to use SMTP 
       $mail->Host = $mail_smtp_host; // Specify main and backup SMTP servers 
       $mail->SMTPAuth = true;        // Enable SMTP authentication 
       $mail->Username = $mail_smtp_username;     // SMTP username 
       $mail->Password = $mail_smtp_password;       // SMTP password 
       $mail->SMTPSecure = $mail_smtp_enc;       // Enable TLS encryption, `ssl` also accepted 
       $mail->Port = $mail_smtp_port;         // TCP port to connect to 

       $mail->setFrom($mail_smtp_from_email, $mail_smtp_from_name); 
       $mail->addAddress($lst['email']);  // Add a recipient 

       $mail->isHTML(true);         // Set email format to HTML 

       $mail->Subject = $mail_subject; 
       $mail->Body = $content; 


       if(!$mail->send()) { 
        echo 'Message could not be sent.'; 
        echo 'Mailer Error: ' . $mail->ErrorInfo; 
       } else { 
        echo 'Message has been sent'; 
       } 
      $lst['email']='';    
     /*************************************************php mailer ends here*************************************/ 


    } ///foreach ends here 
+0

請用var_dump奧德類似於創建調試輸出,什麼是$郵件對象內部和$內每個循環調用lst ['email'] –

+0

將您的代碼基於[PHPMailer提供的郵件列表示例](https://github.com/PHPMailer/PHPMailer/blob/master/examples/mailing_list.phps)。它比你在這裏的效率高得多。 – Synchro

回答

2

在循環中創建新的$mail對象,或者甚至更好地重置循環中的地址。你(可能)在for循環之外聲明瞭$mail對象。

因此,每次撥打電話addAddress時,都會添加用戶的電子郵件地址。第一次設置第一個用戶的電子郵件地址時,第二次將電子郵件地址添加到列表中,因此有兩個電子郵件地址。第三輪相同,第四輪等。

編輯:在循環內創建$mail修復問題,但效率不高。請參閱Synchro的評論,以獲得更好的創建和發送電子郵件的方式。

+0

是的郵件對象是在循環之外。將郵件對象放入循環後它的工作正常。謝謝 – mohit

+0

不要在循環內部創建PHPMailer實例 - 效率低下,您不需要這樣做來解決此問題。看到我對這個問題的評論。 – Synchro

2

無需在每個循環中創建一個PHPMailer實例。在foreach循環的末尾使用$mail->clearAddresses();來清除所有地址。

更新的代碼

$list = $tmp_con->query("SELECT name,email FROM users WHERE user_type='$tp' AND subscribed='yes' AND verified='1'") or die($tmp_con->error); 
$lis = array(); 

while ($row = $list->fetch_array()) { 
    $lis[] = $row; 
} 

$mail->isSMTP();          // Set mailer to use SMTP 
$mail->Host = $mail_smtp_host; // Specify main and backup SMTP servers 
$mail->SMTPAuth = true;        // Enable SMTP authentication 
$mail->Username = $mail_smtp_username;     // SMTP username 
$mail->Password = $mail_smtp_password;       // SMTP password 
$mail->SMTPSecure = $mail_smtp_enc;       // Enable TLS encryption, `ssl` also accepted 
$mail->Port = $mail_smtp_port;         // TCP port to connect to 
$mail->setFrom($mail_smtp_from_email, $mail_smtp_from_name); 

foreach ($lis as $lst) { 
    $content = str_replace("{name}", $lst['name'], $mail_content); 
    $content = $content . "" . $append_unsub; 

    //$mail->SMTPDebug = 3; 
    $mail->addAddress($lst['email']);  // Add a recipient 
    $mail->isHTML(true); // Set email format to HTML 
    $mail->Subject = $mail_subject; 
    $mail->Body = $content; 

    if (!$mail->send()) { 
    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
    } else { 
    echo 'Message has been sent'; 
    } 
    $mail->clearAddresses(); // Clear all addresses for next loop 
} 

參考文獻:

  1. PHPMailer