Jump to content

PHPMailer sends duplicates


ayok

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

Link to comment
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).

 

 

Link to comment
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.

Link to comment
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.

 

 

Link to comment
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++;
}
?>

Link to comment
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().

Link to comment
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!

Link to comment
Share on other sites

  • 2 months later...

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??

Link to comment
Share on other sites

  • 8 months later...

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.