Reputation: 31
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.
<?
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"=>"[email protected]","Mary"=>"[email protected]","Rob"=>"[email protected]");
foreach($r_receivers as $name=>$email){
$mail->SetFrom('[email protected]', "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
The problem get even wierder... I've tried to change the code to just a simple mail(), and it keep sending duplicates. What could be the cause? Is it a server configuration problem??
I did this in the subdomain of mysite.com, say sub.mysite.com. Mysite.com and sub.mysite.com are in different servers. Could it be the cause?
Thanks, ayok
Upvotes: 0
Views: 2226
Reputation: 31
BTW, 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"=>"[email protected]","Mary"=>"[email protected]","Rob"=>"[email protected]");
$i = 1;
foreach($r_receivers as $name=>$email){
$mail->SetFrom('[email protected]', "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++;
}
?>
Upvotes: 0
Reputation: 112
I have worked on the code you posted. I think there is no problem with the code.
I think when ever you refresh your page the mail is fired.
So Please keep the mail code in condition and then check how many mails you get. I hope this answer might help you.
Upvotes: 1