Jump to content

Archived

This topic is now archived and is closed to further replies.

ayok

PHPMailer sends duplicates

Recommended Posts

Hi,

I have this problem like some people with PHPMailer, and I cannot find any solutions on the internet. The problem is that PHPMailer sends duplicates (sometimes more than two) if I set it in a loop (while, foreach). I've checked the loop is just fine, but it keeps sending duplicates.

 

Here is the code after I made it looks simpler.

<?php
require("PHPMailer/class.phpmailer.php");
$select = mysql_query("SELECT * FROM `pm_mailmembers` WHERE `mm_interval`='2' AND mm_blocked = 0") or die(mysql_error());
$mail = new PHPMailer();
$mail->SingleTo = true;
$mail->CharSet = "UTF-8";
$mail->Subject = "Fiscanet Nieuws";
$sendContent = "<p>This is test mail</p>";
$r_receivers = array("John"=>"john@mail.com","Mary"=>"mary@mail.com","Rob"=>"rob@mail.com");
foreach($r_receivers as $name=>$email){
	$mail->SetFrom('no-reply@yoursite.com', "Yoursite");
	$mail->MsgHTML($sendContent);
	$mail->AddAddress($email, $name);
	if($mail->Send())
		echo "Sent to: ".$email."<br/>";
	else
		echo "Not sent to: ".$email.", reason: ".$mail->ErrorInfo."<br/>";
	$mail->ClearAddresses();
}
?>

I think I need to reset mail->addaddress inside the loop, but mail->clearaddresses doesn't help. And all 3 emails always receives the same emails. So all receives 2 or 3 mails.

 

Could anyone help me out here?? Thanks.

ayok

Share this post


Link to post
Share on other sites

I've even tried to set the $mail->send() and the rest out of the loop (except $mail->addaddress), but it keeps sending you duplicates.

Share this post


Link to post
Share on other sites

The AddAddress() method keeps adding addresses to the list of TO: addresses (so that you can send the same email to multiple TO: addresses.). You would need to call the ClearAddresses() method to clear the list of TO: address(es).

 

 

Share this post


Link to post
Share on other sites

Hi. Thanks for your reply.

However, I have put $mail->ClearAddresses() on my code, and it doesn't help. I've also tried to put it before the addAddress().

 

And it's also strange that I got the same amount of duplicates. My logic said, it should send 3 emails to the first and just 1 email for the last. But mostly I received 2 emails on all 3 email addresses.

Share this post


Link to post
Share on other sites

Are you sure the duplicates you are getting aren't the result of previous testing? Change the email content in some way each time (perhaps put the date/time into it) so that you know which emails are from any particular execution of your script.

 

With the ClearAddresses() statement in your code (in your first post, I didn't actually scroll down to see that), the code should only send one mail to each address. Are you sure that is your actual code being executed on the server?

 

If the email body is going to be the same for all recipients, you would generally use a list of BCC: addresses to send the same email to multiple recipients, while hiding the list of addresses from each recipient.

 

 

Share this post


Link to post
Share on other sites

Yes I'm sure. I have tested the script before posting it. And I got duplicates mail. It's confused me why ClearAddresses doesn't help.

 

How can I hide the list of addresses while I use BCC?

Share this post


Link to post
Share on other sites

I've set a time on the content. The duplicates are sent 6 minutes after the first.

I've also set an order number, and all the duplicates has same number.

 

<?php
require("PHPMailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->SingleTo = true;
$mail->CharSet = "UTF-8";
$mail->Subject = "Fiscanet Nieuws";
$r_receivers = array("John"=>"john@mail.com","Mary"=>"mary@mail.com","Rob"=>"rob@mail.com");
        $i = 1;
foreach($r_receivers as $name=>$email){
	$mail->SetFrom('no-reply@yoursite.com', "Yoursite");
	$mail->MsgHTML($sendContent);
	$mail->AddAddress($email, $name);
        $sendContent = "<p>This is test mail number ".$i." sent at ".date('H:i:s')."</p>";
	if($mail->Send())
		echo "Sent to: ".$email."<br/>";
	else
		echo "Not sent to: ".$email.", reason: ".$mail->ErrorInfo."<br/>";
	$mail->ClearAddresses();
                $i++;
}
?>

Share this post


Link to post
Share on other sites

If you are getting emails with time values in them that are 6 minutes apart, then your script is being executed multiple times. You are either requesting/refreshing the page again or you have a cron job/scheduled task that is requesting it with a 6 minute interval.

 

The counter WILL be the same value for any particular email address. All the "John" emails will have a count of 1, all the "Mary" emails will have a count of 2, and all the "Rob" emails will have a count of 3. If you mean that ALL the emails have the same count, I doubt it. What exact count values are you getting at each test email address?

 

Edit: The last code you posted above is setting the $sendContent variable AFTER you have used it in the $mail->MsgHTML() method. So, the first email to the test "John" account isn't going to contain any message body. I recommend re-ordering your code so that it assigns the string to $sendContent before it calls $mail->MsgHTML().

Share this post


Link to post
Share on other sites

If you are getting emails with time values in them that are 6 minutes apart, then your script is being executed multiple times. You are either requesting/refreshing the page again or you have a cron job/scheduled task that is requesting it with a 6 minute interval.

 

Hmm... Maybe I need to check about this.

 

The counter WILL be the same value for any particular email address. All the "John" emails will have a count of 1, all the "Mary" emails will have a count of 2, and all the "Rob" emails will have a count of 3. If you mean that ALL the emails have the same count, I doubt it. What exact count values are you getting at each test email address?

 

So. John = 1, 2 times

Mary = 2, 2 times and

Rob = 3, 2 times.

 

Edit: The last code you posted above is setting the $sendContent variable AFTER you have used it in the $mail->MsgHTML() method. So, the first email to the test "John" account isn't going to contain any message body. I recommend re-ordering your code so that it assigns the string to $sendContent before it calls $mail->MsgHTML().

 

I'll check this one too..

Thanks!

Share this post


Link to post
Share on other sites

Hi..

I'm still struggling with this problem.

I've found out that it doesn't send duplicates when I use ajax to call this page, but if I open the page or refresh it (cos that's how the cron job works) it sends duplicates...

 

What does make it different with refresh and call it with ajax??

Share this post


Link to post
Share on other sites

Problem Resolved:

 

Guys, here is the solution. the problem is with our object. It is not getting clear after sending the email, so it ads the second email again in TO or CC list and hence user gets duplicate email. Check the code below.

 

protected function AddAnAddress($kind, $address, $name = '') {
  $this->$kind= array();//initialize becuase it is apeending email together and sending multiple times
    if (!preg_match('/^(to|cc|bcc|Reply-To)$/', $kind)) {
      $this->SetError($this->Lang('Invalid recipient array').': '.$kind);

 

Add one line of code in your function AddAnAddress() , as mentioned above in RED color and your problem will get resolve.

 

That is all :)

 

Enjoy programming.

 

- DJ

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.