PHPMailer 5.1 sends duplicatie mails when adding more than 1 receiver

穿精又带淫゛_ 提交于 2019-12-04 05:14:36

You can use $SingleTo property.

From PHPMailer docs:

$SingleTo

Provides the ability to have the TO field process individual emails, instead of sending to entire 'TO addresses'

$mail = new PHPMailer();

$mail->SingleTo = true; //will send mail to each email address individually


$mail->AddReplyTo("name@yourdomain.com","First Last");
$mail->SetFrom('name@yourdomain.com', 'First Last');

$mail->AddAddress("info@address.com");
$mail->AddAddress("info@address.nl");
$mail->Subject    = "PHPMailer Test Subject via mail(), basic"; 

$mail->Send();

Try to add after

$mail->send();


$mail->ClearAddresses();

Ok, if someone runs into this problem like I did:

My website runs in utf-8, PHPMailer uses iso-8859-1 by default. Somehow the script is run in both charsets causing duplicates. Changing the charset in the class.phpmailer.php file to utf-8 solved the problem!

SingleTo Is not a good idea. It only works with "sendmail" or "mail" transports, not with SMTP. If you use SingleTo with SMTP, this parameter is just ignored without any error or warning, and you may get duplicates.

According to the authors of the 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: "PHPMailer isn't a mailing list sender". They tell that the use of the PHP mail() function needs to be discouraged because it's extremely difficult to use safely; SMTP is faster, safer, and gives more control and feedback.

But SMTP is incompatible with SingleTo -- that's why the authors of PHPMailer will remove SingleTo, not SMTP.

I think these will do the job :)

$mail->SingleTo   = true;
$mail->CharSet    = "UTF-8";

I tried everything, UTF8 / BOM check on all files, SingleTo-property, CharSet-property. It didn't work.

I used the "View Details" function from Gmail and saw that my duplicate mails were sent without a subject.

So, I ended up with a very, very dirty fix. I put this DIRECTLY below the first line of the send function in the class file:

public function Send() {
  if ($this->Subject == "") {
    return true;
  }
  // ... rest of code...
}

I ran into the same problem. The one and only right answer is $mail->ClearAllRecipients() When I used the accepted answer BCC mails were being sent to all recipients.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!