2014-11-24 17 views
0

我試圖從數據庫發送電子郵件。我從數據庫中收到電子郵件,並將消息發送給我從數據庫中獲得的電子郵件。如何在PHP中發送多個郵件?

我的代碼運行良好,但發送郵件時發生問題 - 只發送第一個郵件。

include("config.php"); 
include ("library.php"); 
include ("classes/class.phpmailer.php"); 


$sql = "select * from Table "; 
$result = mysql_query($sql, $conn); 
while ($row = mysql_fetch_array($result)) { 
if ($row['Date_Registry'] == date("Y-m-d")){ 
$sql= "select * from write_message where ID='$m'"; 
$result = mysql_query($sql, $conn); 
$row = mysql_fetch_array($result); 
$email= $row['EMAIL']; 
$masg = $row['Write_message']; 
$Message_name = $row['Message_name']; 
    $email = $email; 
    $mail = new PHPMailer; // call the class 
    $mail->IsSMTP(); 
    $mail->Host = SMTP_HOST; //Hostname of the mail server 
    $mail->Port = SMTP_PORT; //Port of the SMTP like to be 25, 80, 465 or 587 
    $mail->SMTPAuth = true; //Whether to use SMTP authentication 
    $mail->Username = SMTP_UNAME; //Username for SMTP authentication any valid email created in your domain 
    $mail->Password = SMTP_PWORD; //Password for SMTP authentication 
    $mail->SetFrom(SMTP_UNAME, "Helli"); //From address of the mail 
    // put your while loop here like below, 
    $mail->Subject = $Message_name; //Subject od your mail 
    $mail->AddAddress($email); //To address who will receive this email 

} 
} 
+1

難道你不需要調用send()方法嗎? – halfer 2014-11-24 09:30:34

+1

注意:'mysql_ *'函數已被棄用,它們將在未來版本中從PHP中刪除,並且您的代碼將停止工作。您不應使用它們編寫新代碼,而應使用['mysqli_ *'或PDO](http://php.net/manual/en/mysqlinfo.api.choosing.php)。 – 2014-11-24 09:31:39

回答

0

只需在AddAddress函數上添加函數clearAddress(如下所示),它就可以工作。

include("config.php"); 
include ("library.php"); 
include ("classes/class.phpmailer.php"); 


$sql = "select * from Table "; 
$result = mysql_query($sql, $conn); 
while ($row = mysql_fetch_array($result)) { 
if ($row['Date_Registry'] == date("Y-m-d")){ 
$sql= "select * from write_message where ID='$m'"; 
$result = mysql_query($sql, $conn); 
$row = mysql_fetch_array($result); 
$email= $row['EMAIL']; 
$masg = $row['Write_message']; 
$Message_name = $row['Message_name']; 
    $email = $email; 
    $mail = new PHPMailer; // call the class 
    $mail->IsSMTP(); 
    $mail->Host = SMTP_HOST; //Hostname of the mail server 
    $mail->Port = SMTP_PORT; //Port of the SMTP like to be 25, 80, 465 or 587 
    $mail->SMTPAuth = true; //Whether to use SMTP authentication 
    $mail->Username = SMTP_UNAME; //Username for SMTP authentication any valid email created in your domain 
    $mail->Password = SMTP_PWORD; //Password for SMTP authentication 
    $mail->SetFrom(SMTP_UNAME, "Helli"); //From address of the mail 
    // put your while loop here like below, 
    $mail->Subject = $Message_name; //Subject od your mail 

    $this->phpMailerObj->clearAddresses(); //Clear Addresses 

    $mail->AddAddress($email); //To address who will receive this email 
    $this->phpMailerObj->Body = "body"; 
    $this->phpMailerObj->msgHTML("Message"); 
    try { 
     $this->phpMailerObj->Send(); 
     return; 
    } catch (Exception $exc) { 
     return $error['error'] = "Email cound not be sent"; 
    } 

} 
}