2011-03-08 45 views
0

我想寫PHPMailer發送電子郵件。我使用mysqli_fetch_array的while循環從MySQL中提取電子郵件記錄,並將'email'數據庫字段分配給一個名爲'$ to'的變量,並提供給PHPMailer $ mailer-> AddAddress(「[email protected]」)呼叫。

該腳本可以工作,但僅向數據庫中發現的第一個收件人發送電子郵件。任何線索我在哪裏搞砸了?謝謝!

$from = '[email protected]'; 
    $from_name = 'My Name'; 
    $subject = $_POST['subject']; 
    $text = $_POST['elvismail']; 

$dbc = mysqli_connect('localhost', 'username', 'the_password', 'database_name') 
    or die('Error connecting to mysql'); 

$query = "SELECT * FROM email_list"; 
$result = mysqli_query($dbc, $query) 
    or die('Error querying database.'); 

    while ($row = mysqli_fetch_array($result)){ 
     $to = $row['email']; 
     $first_name = $row['first_name']; 
     $last_name = $row['last_name']; 
     $msg = "Dear $first_name $last_name,\n$text"; 
    } 


    require("PHPMailer_v5.1 2/class.phpmailer.php"); 
    $mailer = new PHPMailer(); 
    $mailer->IsSMTP(); 
    $mailer->Host = 'ssl://smtp.gmail.com:465'; 
    $mailer->SMTPAuth = TRUE; 
    $mailer->Username = '[email protected]'; // Sender's gmail address 
    $mailer->Password = 'the_password'; // Sender's gmail password 
    $mailer->From = "$from"; // Sender's email address 
    $mailer->FromName = "$from_name"; // senders name 
    $mailer->Body = "$msg"; 
    $mailer->Subject = "$subject"; 
    $mailer->AddAddress("$to"); // Recipient 
     if(!$mailer->Send()) 
    { 
     echo 'Email sent to:' . $to . '<br/ >'; 
     echo "Mailer Error: " . $mailer->ErrorInfo; 
    } 
    else 
    { 
     echo 'Email sent to:' . $to . '<br/ >'; 
    } 


    mysqli_close($dbc); 
+0

這已在此處回答。 看看http://stackoverflow.com/questions/1770765/phpmailer-addaddress – 2011-03-08 23:56:42

回答

1

您正在儘快關閉while循環。

將其更改爲:

require("PHPMailer_v5.1 2/class.phpmailer.php"); 
while ($row = mysqli_fetch_array($result)){ 
    $to = $row['email']; 
    $first_name = $row['first_name']; 
    $last_name = $row['last_name']; 
    $msg = "Dear $first_name $last_name,\n$text"; 

    $mailer = new PHPMailer(); 
    $mailer->IsSMTP(); 
    $mailer->Host = 'ssl://smtp.gmail.com:465'; 
    $mailer->SMTPAuth = TRUE; 
    $mailer->Username = '[email protected]'; // Sender's gmail address 
    $mailer->Password = 'the_password'; // Sender's gmail password 
    $mailer->From = "$from"; // Sender's email address 
    $mailer->FromName = "$from_name"; // senders name 
    $mailer->Body = "$msg"; 
    $mailer->Subject = "$subject"; 
    $mailer->AddAddress("$to"); // Recipient 
    if(!$mailer->Send()) 
    { 
     echo 'Email sent to:' . $to . '<br/ >'; 
     echo "Mailer Error: " . $mailer->ErrorInfo; 
    } 
    else 
    { 
     echo 'Email sent to:' . $to . '<br/ >'; 
    } 


// Then close your while loop here 
} 

mysqli_close($dbc); 
+0

謝謝!我得到了它的工作。 – ven 2011-03-22 18:20:33

+0

不客氣。不要忘記上傳並接受答案。這是如何顯示堆棧溢出的讚賞! – webbiedave 2011-03-23 00:22:46

0

在你的循環,$to(和其他變量)的結束將被設置爲循環的最後一次迭代。

調整您的代碼,讓您在循環上方初始化PHPMailer,然後在循環中調用$mailer->AddAddress($to)

不要忘了在循環完成後請致電$mailer->Send() :)

+0

感謝您的幫助alex! – ven 2011-03-22 18:21:26

相關問題