ayok Posted June 18, 2012 Share Posted June 18, 2012 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 Quote Link to comment Share on other sites More sharing options...
ayok Posted June 18, 2012 Author Share Posted June 18, 2012 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. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 18, 2012 Share Posted June 18, 2012 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). Quote Link to comment Share on other sites More sharing options...
ayok Posted June 18, 2012 Author Share Posted June 18, 2012 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. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 18, 2012 Share Posted June 18, 2012 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. Quote Link to comment Share on other sites More sharing options...
ayok Posted June 18, 2012 Author Share Posted June 18, 2012 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? Quote Link to comment Share on other sites More sharing options...
ayok Posted June 19, 2012 Author Share Posted June 19, 2012 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++; } ?> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 19, 2012 Share Posted June 19, 2012 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(). Quote Link to comment Share on other sites More sharing options...
ayok Posted June 19, 2012 Author Share Posted June 19, 2012 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! Quote Link to comment Share on other sites More sharing options...
ayoksus Posted August 22, 2012 Share Posted August 22, 2012 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?? Quote Link to comment Share on other sites More sharing options...
deepsgjain Posted May 20, 2013 Share Posted May 20, 2013 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.