douzhaxian1267 2013-07-09 05:22
浏览 151

使用phpmailer发送批量电子邮件

i am trying to send bulk email using phpmailer i have more than 10 email id's in my database when i click on send button then first email will go to one person, the second email sent will go to that same person plus another, the third one will go to those two plus one more, and so on. this is my coding please help me

<?php

$body=$_POST['message'];
$subject=$_POST['sub'];

//error_reporting(E_ALL);
error_reporting(E_STRICT);

date_default_timezone_set('America/Toronto');

require_once("class.phpmailer.php");
//include("class.smtp.php");


$mail             = new PHPMailer();

$mail->IsSMTP(); // telling the class to use SMTP

$mail->Host       = "stmp.gmail.com"; // SMTP server

$mail->SMTPDebug  = 1;                     // enables SMTP debug information

// 1 = errors and messages

// 2 = messages only

$mail->SMTPAuth   = true;                  // enable SMTP authentication

$mail->SMTPSecure = 'ssl'; 

$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server

$mail->Port       = 465;                   // set the SMTP port for the GMAIL server

$mail->CharSet = "big5";

$mail->Username   = "abc@gmail.com";  // GMAIL username

$mail->Password   = "**********";            // GMAIL password

$mail->SetFrom("abc@gmail.com", ''); // set reply id

$mail->Subject    = ($subject); // subject

$mail->MsgHTML("$body"); // message 

$mail->AddAddress($address, "abc");


$con=mysql_connect("localhost","root","") or
die("could not connect:".mysql_error());

mysql_select_db("bulkemail");
$qry=mysql_query("SELECT * FROM email_id", $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}


while($row = mysql_fetch_array($qry))

{

$id= $row["email"];

$address = ($id);

$mail->AddBcc($id);

$mail->send();

if(!$mail->Send()) {
 echo "Mailer Error: " . $mail->ErrorInfo;
}
 else {
 echo "Message sent!";
}
}
?>
  • 写回答

3条回答 默认 最新

  • ds000001 2013-07-09 05:28
    关注

    Remove $mail->send(); and move if(!$mail->Send()) to outside the while($row ..) loop

    while($row = mysql_fetch_array($qry)){
       $id= $row["email"];
       $address = ($id);
       $mail->AddBcc($id);    
    } // end the while loop
    
    // remove $mail->send(); as it is a duplicate of if(!$mail->Send()) 
    
    if(!$mail->Send()) {
       echo "Mailer Error: " . $mail->ErrorInfo;
    }
    else {
       echo "Message sent!";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多