Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPMailer 5.1 sends duplicatie mails when adding more than 1 receiver

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("[email protected]","First Last");
$mail->SetFrom('[email protected]', 'First Last');

$mail->AddAddress("[email protected]");
$mail->AddAddress("[email protected]");
$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 :)

like image 557
Joshua - Pendo Avatar asked Feb 13 '12 17:02

Joshua - Pendo


People also ask

How can I send multiple emails in PHPMailer?

// First Email $to = array( '[email protected]', '[email protected]',); $subject = "Subject"; $message = $message_start. $message_ONE. $message_end; sendMail(); // Second Email $to = array( '[email protected]', '[email protected]',); $subject = "Subject"; $message = $message_start.

Does PHPMailer use SMTP?

PHPMailer provides powerful functionality to create HTML emails with attachments and send them to multiple recipients via SMTP or a local webserver.

Does PHPMailer work on localhost?

The way you have setup your PHPMailer, it would require an SMTP server running on your localhost to send the messages. If you don't have an SMTP server running on your localhost, then you can use an external SMTP server to relay the messages through.

What is addReplyTo PHPMailer?

I have found the answer to this, and it is annoyingly/frustratingly simple! Basically the reply to addresses needed to be added before the from address as such: $mail->addReplyTo('[email protected]', 'Reply to name'); $mail->SetFrom('[email protected]', 'Mailbox name');


2 Answers

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("[email protected]","First Last");
$mail->SetFrom('[email protected]', 'First Last');

$mail->AddAddress("[email protected]");
$mail->AddAddress("[email protected]");
$mail->Subject    = "PHPMailer Test Subject via mail(), basic"; 

$mail->Send();
like image 161
Zul Avatar answered Nov 09 '22 23:11

Zul


Try to add after

$mail->send();


$mail->ClearAddresses();
like image 33
Toux Avatar answered Nov 10 '22 01:11

Toux