Home » Php » send multiple email using phpmailer, i am using Gmail SMTP – PHP

send multiple email using phpmailer, i am using Gmail SMTP – PHP

Posted by: admin February 23, 2020 Leave a comment

Q(Question):

i am trying to send multiple mail using phpmailer. I have a problem. When i click send button, just [email protected] and [email protected] receives the message.

How to change it? I’m use Gmail SMTP send out.

There are 5 records in the database:

Here is my code:


<?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");
$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'];
$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 (for testing)
// 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->AddReplyTo("[email protected]","");
$mail->Subject = ($subject); // subject
$mail->AltBody = "Hey, check out this new post on www.techaltum.in"; // optional, comment out and test
$mail->MsgHTML("$body"); // message
$address = ($id);
$mail->AddAddress($address);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
?>

A(Answer):

Line#33 is wrong:

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

should it not be:

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

?

oooh, wait, that is corrected on line #45 😉

A(Answer):

What is the output of this script?

Is it 5 times "Message sent!" ?

Is there any extra output for line #35 "$mail->SMTPDebug = 1;" ??

A(Answer):

There is 5 e-mail id’s are in my Database but only 2 e-mail id’s receive mail but other one didn’t receive mail

A(Answer):

I just copied your script

changed all references to ‘[email protected]’ to [email protected]

changed line4 to: $body="test";
changed line5 to: $subject="test";

change database stuff at line #15 and #18 to match my database

entered 5 emailaddresses in this table…

and it worked…..

I therefore see two options:
1) your password is wrong (line #35)
(re-check your password)
2) emailaddresses in database are wrong.
maybe change line #27 to check the addresses:

$id= $row['email']; echo $id;

A(Answer):

i have check all my code which you suggest but still i am not able to send more than 2 mail

A(Answer):

Again: What is the output of this script?

Or, what happens if you put these addreses in your database:
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

You should receive those 5 messages in the box of [email protected]

A(Answer):

the output is
[email protected] Message sent!
[email protected] Message sent!
[email protected]
[email protected]
[email protected]

and only 2 email id receive mail

A(Answer):

hmmzzzzz, why did you leave out the ‘+’ ??

But i’m afraid i don’t see an answer to your problem…..

A(Answer):

can you send me you coding

A(Answer):

it’s the same as your code, just different emailaddresses
and i downloaded phpmailer from here

A(Answer):

now i can send email to more people but now i am getting new problem when i send mail 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.

A(Answer):

That’s because on line 67 of your original code, you call AddAddress. That will add an address to the To line, it doesn’t replace what’s there. You need to call ClearAddresses() before you add an address.

A(Answer):

@Rabbit:
i thought it wen ok, because of the ‘new’ in line #29:

$mail = new PHPMailer();

A(Answer):

Refer to the documentation’s example here: http://phpmailer.worxware.com/index.php?pg=exampledb. Notice their use of ClearAddresses at the bottom. There is no need to keep creating instances and setting values that only need to be set once.