PHPMailer 5.1 sends duplicatie mails when adding more than 1 receiver

隐身守侯 提交于 2019-12-09 16:59:16

问题


I'm using PHPMailer for a while now and never really had any problems, but last week I installed one of my CMS on a website and the client insisted on having 2 e-mails receiving the contents of the contact form of his website.

Ok, no problem I thought, just adding an e-mail address using the $phpmailer->AddAddress() function. However, adding a second receiver is causing PHPMailer to send the mail twice to both receivers. I tried adding a third receiver to see if I got it three times, but this didn't change anything. So adding 2+ receivers is causing PHPMailer to send the message twice to all receivers.

There's nothing strange in my code. It's a basic PHPMailer example:

$mail             = new PHPMailer();
$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();

I've ran out of options. I have absolutely no clue where it's going wrong.

Thanks in advance

--

Just some random thought: I noticed the mailer is defaulted by iso-8859-1 and my site is running utf8. Can there be a "silent" redirect by the server itself?

//EDIT, that ^^ solved my problem

//EDIT2:

Ok, it did not.. today the script worked fine (just 1 mail with 2 receivers) but a moment ago the strange behavior started again. Any tips?

// Solution:

ok, I feel quiet stupid! The answer Zulkhaery Basrul came closest to the actual problem! This is what happened: I was sending an e-mail to multiple addresses in the "to" field. For example:

To: A, B, C

Both A/B are my own adresses. In outlook I had some message rules to put e-mails with certain addressees inside a specific folder. Both A and B had this rule.

When I recieved my e-mail, both mails contained both mailaddresses in the to-field. Causing both of the mails to meet the rule requirements for both rules. Thus keeping the original in one folder and creating a copy in the other (twice).

Thanks for thinking tho :)


回答1:


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();



回答2:


Try to add after

$mail->send();


$mail->ClearAddresses();



回答3:


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!




回答4:


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.




回答5:


I think these will do the job :)

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



回答6:


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...
}



回答7:


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.



来源:https://stackoverflow.com/questions/9264693/phpmailer-5-1-sends-duplicatie-mails-when-adding-more-than-1-receiver

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