如何用PHP发送多个邮件?

时间:2014-11-24 09:28:47

标签: php email

我试图从数据库发送电子邮件。我从数据库收到电子邮件,并将消息发送到我从数据库获得的电子邮件。

我的代码工作正常,但发送邮件时遇到问题 - 只发送给第一个邮件。

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 个答案:

答案 0 :(得分: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";
    }

}
}
相关问题