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 :)
// 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.
PHPMailer provides powerful functionality to create HTML emails with attachments and send them to multiple recipients via SMTP or a local webserver.
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.
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');
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();
Try to add after
$mail->send();
$mail->ClearAddresses();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With