2012-01-07 17 views
0

我想修改phpmailer數據庫郵件使用sendmail,這個代碼是正確的?我正在嘗試將它實現到一個網站,通過電子郵件發送大約2-300個聯繫人的客戶羣。 感謝:-)正確的方式發送郵件()與phpmailer使用數據庫連接

<?php 
require_once('./send/class.phpmailer.php'); 
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php  if not already loaded 

$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it  will throw exceptions on errors, which we need to catch 

$body    = file_get_contents('contents.html'); 
$body    = eregi_replace("[\]",'',$body); 


$mail->SetFrom('[email protected]', 'List manager'); 
$mail->AddReplyTo('[email protected]', 'List manager'); 

$mail->Subject  = "PHPMailer Test Subject via smtp, basic with authentication"; 

$query = "SELECT full_name, email, photo FROM employee WHERE id=$id"; 
$result = @MYSQL_QUERY($query); 

while ($row = mysql_fetch_array ($result)) { 
    $mail->AltBody = "To view the message, please use an HTML compatible email viewer!";  // optional, comment out and test 
    $mail->MsgHTML($body); 
    $mail->AddAddress($row["email"], $row["full_name"]); 
    $mail->AddStringAttachment($row["photo"], "YourPhoto.jpg"); 

    if(!$mail->Send()) { 
    echo "Mailer Error (" . str_replace("@", "&#64;", $row["email"]) . ') ' .   $mail->ErrorInfo . '<br>'; 
    } else { 
    echo "Message sent to :" . $row["full_name"] . ' (' . str_replace("@", "&#64;",  $row["email"]) . ')<br>'; 
} 
// Clear all addresses and attachments for next loop 
$mail->ClearAddresses(); 
$mail->ClearAttachments(); 
} 
?>  
+0

你有這個代碼的實際問題? – 2012-01-07 13:15:02

+0

不,我剛剛合併了使用郵件功能的簡單郵件程序腳本和使用smtp函數的db郵件程序,並刪除了smtp部分,並從另一個腳本添加了sendmail部分,我只是確保我的代碼亂碼沒問題:-)。 – 2012-01-07 13:27:38

+0

在腳本中說它的id = $ id,我是否必須定義$ id,因爲我無法在腳本或phpmailer.php中找到任何其他引用 – 2012-01-07 13:32:48

回答

1

這應該工作,但要確保該功能是由託管公司的支持。