dongpang2029 2014-11-24 09:28
浏览 148

如何用PHP发送多个邮件?

I'm trying to send email from a database. I get email from the databse and send message to email I get from database.

My code is working good, but I have problem the sending mail - it is only sent for the first one.

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条回答 默认 最新

  • duangengruan2144 2014-11-24 09:35
    关注

    Just add the function clearAddress above the AddAddress function (as below) and it will work.

    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";
        }
    
    }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题