I couldn’t find the answer to my specific scenario.
I am developing a system to send out publication to a mailing list from database. I have managed to do it using normal loop code.
However, I want to use loop only to add recipients using BCC and also maintain error handling if anyone has missed it something like:
foreach($array as $user){
$mail->AddBCC( $user['email'], $user['customerName']);
}
try{
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment
$mail->AddReplyTo("noreply@company.com","Company Name");
$mail->SetFrom('noreply@company.com', 'Company Name');
$mail->Subject = "Company| E-Zine";
$mail->MsgHTML($ezineContent);
if(!$mail->Send()) {
//show error msg
} else {
//show successful msg
}
}catch (phpmailerException $e) {
//show error msg
}catch (Exception $e) {
//show error msg
}
$mail->ClearAddresses();
Then I want to send the e-mail to all recipients added above using one call.
Is it possible to do error handling and find if anyone hasn’t received it because the address wasn’t correct??
A common practice of tracking the success of mailing operations is to use the “Return-Path” header of an email.
Example of an email and its header:
Whereby the “From” header is your choice of real name and email address that you want the reader to see, the primary purpose of the “Return-Path” is to designate the address to which messages indicating non-delivery or other mail system failures are to be sent ([see RFC 2821 for more details][1]).
So basically this header is the right position to start fetching non-delivery reports.
How I would do this:
Here an example with a unique Return-Path:
That’s it.
Edit – How to implement that via phpMailer:
As far as I remember we had problems by using the local sendmail. The header Return-Path was replaced by some configurations of the local MTA. If this is the case try to use SMTP to a usable relay host instead.