2014-11-24 44 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"; 
    } 

} 
}