afroz ahmad
afroz ahmad

Reputation: 21

Sending bulk e-mail using 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   = "[email protected]";  // GMAIL username

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

$mail->SetFrom("[email protected]", ''); // 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!";
}
}
?>

Upvotes: 2

Views: 6285

Answers (3)

Exogenus
Exogenus

Reputation: 124

In your case, you are adding a new BCC to the list of all BCC already send in every repeat of the loop.

Like "kworr Sep 27 '13 at 18:36 " said, you need to create new instance for every sending in the loop.

Or you need to put

$mail->send();

out of the loop.

Upvotes: 0

terek
terek

Reputation: 21

I had used phpmailer for mass mailing and I had same problem.

while($row = mysql_fetch_array($qry)){
  $id= $row["email"];
  $address = ($id);
  $mail->AddBcc($id);  

//imo if should be in the while loop.

  if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
  }
  else {
       echo "Message sent!";
//I added this function down there so that old address would be removed but in the new loop, new one will be added.
       $mail-> ClearAddresses();
  }  
} // end the while loop

This works for me.

Upvotes: 2

Sean
Sean

Reputation: 12433

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!";
}

Upvotes: 4

Related Questions