Reputation: 31
I've got this problem since two months ago, and still strugling with it. I'm using PHPMailer for my mailinglist program. And I have a cron job which runs in certain times. However, there is a problem with the emails.
I use PHPMailer in a loop where I send to all of the mailinglist members. The codes look like this:
<?php
require("PHPMailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->SingleTo = true;
$mail->CharSet = "UTF-8";
$mail->Subject = "Our news";
$r_receivers = array("John"=>"[email protected]","Mary"=>"[email protected]","Rob"=>"[email protected]");
foreach($r_receivers as $name=>$email){
$mail->SetFrom('[email protected]', "Oursite");
$mail->MsgHTML($sendContent);
$mail->AddAddress($email, $name);
$sendContent = "<p>E-mail body</p>";
if($mail->Send())
echo "Sent to: ".$email."<br/>";
else
echo "Not sent to: ".$email.", reason: ".$mail->ErrorInfo."<br/>";
$mail->ClearAddresses();
}?>
when I call this codes with ajax, it works perfect. However, if I execute this codes in a browser or refresh it or call it with cron job, it sends me duplicates.
Can someone explain me why is it going wrong when I open it with browser/refresh? Why I got different result by calling it with ajax and calling it from browser?
Upvotes: 2
Views: 827
Reputation: 4782
You are using SingleTo, which is discouraged. According to the authors of the PHPMailer library, SingleTo is planned to be deprecated in the release of PHPMailer 6.0, and removed in 7.0. The authors have explained that it's better to control sending to multiple recipients at a higher level, since PHPMailer isn't a mailing list sender. They tell that the use of the PHP mail() function needs to be discouraged also, because it's extremely difficult to use safely; SMTP is faster, safer, and gives more control and feedback. And since SMTP is incompatible with SingleTo, the authors of PHPMailer will remove SingleTo, not SMTP.
Upvotes: 0
Reputation: 366
There appears to be an open issue related to this: http://code.google.com/a/apache-extras.org/p/phpmailer/issues/detail?id=31. You may want to bring your copy of PHPMailer up to the latest version and try again. The issue status is "fixed", though it appears from the comments in the issue ticket that the problem may still exist.
Upvotes: 1