2

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!";
}
}
?>
Micha
  • 5,117
  • 8
  • 34
  • 47
afroz ahmad
  • 21
  • 2
  • 3

3 Answers3

4

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!";
}
Sean
  • 12,443
  • 3
  • 29
  • 47
  • if it is sending multiple times, then `$mail->send()` is either still inside a loop, or you have duplicate email addresses in your database. Once you put `$mail->send()` at the end of you script, like above, it should only send it once to all the bcc recipients. – Sean Jul 09 '13 at 05:38
  • Scratch that. By using both `$mail->send();` and `if(!$mail->Send())` you are most likely sending it twice. remove `$mail->send();` – Sean Jul 09 '13 at 05:39
  • @afrozahmad fyi your code is vulnerable to sql injection and you are using deprecated mysql_* api use pdo instead see this http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/14110189#14110189 – NullPoiиteя Jul 09 '13 at 05:52
  • @NullPoiиteя I doubt it is open to sql injection, as there is no db insertion, or user data in the select query. It is open for xss though. – Sean Jul 09 '13 at 05:58
  • @Sean oops i just made assumption when i saw $subject=$_POST['sub']; and mysql query without _real_escape_string sorry its my fault .. – NullPoiиteя Jul 09 '13 at 06:01
  • @NullPoiиteя your recommendation to change from `mysql_*` is valid though. – Sean Jul 09 '13 at 06:02
  • thank's @ Sean and @NullPoiиteя my query has been resolved now i can send multiple email – afroz ahmad Jul 09 '13 at 06:27
2

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.

kworr
  • 3,579
  • 1
  • 21
  • 33
terek
  • 21
  • 1
  • There's a good example on the phpmailer site: http://phpmailer.worxware.com/index.php?pg=exampledb – kworr Sep 27 '13 at 18:37
0

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.

Exogenus
  • 99
  • 2
  • 6