2012-07-04 164 views
0

我一直有問題使用phpmailer發送批量電子郵件,雖然沒有一個電子郵件的問題。如何發送電子郵件到從其他數據庫中獲取數據庫的電子郵件列表

這裏是我的代碼:

$result = mysql_query("select * from $to",$conn) or die("list 
      selected ($to) does not exist ".mysql_error()); 

while ($row = mysql_fetch_array($result)) 
{   
    $email[] = $row['email'];   
    $student[] = $row['name']; 
} 

foreach ($email as $val => $uemail) { 
    $email = $uemail; 
    $students= $student[$val] ; 

    require("class.phpmailer.php"); 
     $mail = new PHPMailer(true); 

    try { 
      $mail->AddReplyTo('[email protected]', 'My Name'); 
      $mail->AddAddress("$email", "$student"); 
      $mail->SetFrom('[email protected]', 'MyName'); 
      $mail->AddReplyTo('[email protected]', 'My nameg'); 
      $mail->Subject = "$sub"; 

      $mail->MsgHTML("Dear $student<br> $msg <br> 
      <img src=\"$path\"> <p> 

      $host_upper 
      ______________________________________________________ 
      THIS IS AN AUTOMATED RESPONSE. 
      ***DO NOT RESPOND TO THIS EMAIL**** 

      "); 
      $mail->AddAttachment("$path2");  // attachment 

      $mail->Send(); 
      echo "Message Sent OK to $email </p>\n"; 
    } catch (phpmailerException $e) { 
     echo $e->errorMessage(); //Pretty error messages from PHPMailer 
    } catch (Exception $e) { 
     echo $e->getMessage(); //Boring error messages from anything else! 
    } 
} 

任何幫助,建議將不勝感激。

回答

0

分配$student[$val]$students在foreach內,但隨後分配 陣列$student您PHPMailer的對象:

$mail->AddAddress("$email", "$student"); 

不該它是

$mail->AddAddress("$email", "$students"); 

除此之外,爲每個必須發送的電子郵件實例化一個新的郵件對象是一個不好的實踐,您應該在動態變量(如AddAddress)上註冊並保留所有其他外部以避免重載,並記住清除將會發生的變量如AddAddress,像這樣:

require_once("class.phpmailer.php"); 
$result = mysql_query("select * from $to",$conn) or die("list selected ($to) does not exist ".mysql_error()); 

      while ($row = mysql_fetch_array($result)) 
    { 

     $email[] = $row['email']; 

     $student[] = $row['name']; 
    } 

$mail = new PHPMailer(true); 
try { 
      $mail->AddReplyTo('[email protected]', 'My Name'); 
      $mail->SetFrom('[email protected]', 'MyName'); 
      $mail->AddReplyTo('[email protected]', 'My nameg'); 
      $mail->Subject = "$sub"; 
      $mail->AddAttachment("$path2"); 
      foreach ($email as $val => $uemail) { 
       $email = $uemail; 
       $students= $student[$val] ; 

       $mail->AddAddress("$email", "$students"); 
       $mail->MsgHTML("Dear $student<br> $msg <br> 
       <img src=\"$path\"> <p> 

      $host_upper 
      ______________________________________________________ 
      THIS IS AN AUTOMATED RESPONSE. 
      ***DO NOT RESPOND TO THIS EMAIL**** 

      "); 
       $mail->Send(); 
       $mail->ClearAddresses(); 
      echo "Message Sent OK to $email </p>\n"; 
       } 
      } catch (phpmailerException $e) { 
     echo $e->errorMessage(); //Pretty error messages from PHPMailer 
      } catch (Exception $e) { 
      echo $e->getMessage(); //Boring error messages from anything else! 
      } 
      } 
+0

謝謝。完善 – kplus

1

您可以發送郵件while循環中:

<?php 
require_once("class.phpmailer.php"); 

$result = mysql_query('SELECT `email`, `student`, `data1` FROM `students` ORDER BY `email` ASC;'); 

while ($row = mysql_fetch_assoc($result)) 
{ 
    $current_mail = new PHPMailer(true); 

    $current_mail->AddReplyTo('[email protected]', 'My Name'); 
    // .... 
    $current_mail->Send(); 
    unset($current_mail); 
} 
?> 
+0

謝謝我不夠明智。現在完美 – kplus

相關問題