开发者

Handling mail failures using PHP?

开发者 https://www.devze.com 2023-02-04 21:21 出处:网络
If I send an email through GMail to this address nkhkhlkhlkjlkjkljlkjlk@gmail.com I got error like: Delivery to the following recipie开发者_开发问答nt failed permanently

If I send an email through GMail to this address nkhkhlkhlkjlkjkljlkjlk@gmail.com

I got error like:

Delivery to the following recipie开发者_开发问答nt failed permanently

My question is, if I send using the PHP mail function, how can I catch bounce emails?

My code:

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

$to = "nkhkhlkhlkjlkjkljlkjlk@gmail.com";
$subject = "Testing";
$message = "Testing body";

mail($to, $subject, $message, $headers); 


You can't detect such errors just by using mail(). You have to specify a valid return address and then connect to that mailbox (via IMAP or POP, there are functions for that) and check if any message bounced. Bear in mind that bounces may take a long time (even hours, due to transient errors) so you have to do this check asynchronously.


you could get the message by mail when you use this:

$bounce = 'test@domian.com';
mail($to, $subject, $message, $headers,"-f $bounce");

This will send all bounce back mails to your address


You could use PEAR MAil, it contains a parseAddressList() method which checks if the server of an email address accepts the email address. It also returns useful error messages.


The PHP mail() function does have an error status as the function return value, but it won't pick up this kind of error because it only checks that the email has been sent, not that it has been received.

(this is because it needs to return control back to the program immediately; it can't wait for a possible bounce message because they can take a long time to come back - you wouldn't want your program to sit and wait for five days just in case the email bounced, would you?)

Therefore, the only way to determine whether a message has bounced is to have a separate program which checks the mailbox which the bounces would be sent to.

You can specify the mailbox for bounces using the -f option in the mail options string (see the PHP mail() function man page for more info on this).

Then have a separate program periodically check that mailbox for bounce messages, and report them to you as required. (there are a number of PHP libraries that allow you to check a mailbox; Google will help here, or look in PEAR)

Determining which email it was will depend on the quality of the bounce message. You should definitely be able to see the intended recipient's email address, but you may not be able to tell which email it was, ie to match it up to a specific instance of when you called the mail() function in the first place.

Hope that helps.


You should login via IMAP or POP3 to your inbox and parse the email address and delivery error.

It's also worth mentioning that you can specify (while sending) custom headers to better track the message source, I believe MailChimp and others do this do track campaigns, etc...


PHP does not know anything about the mail after it got out of the mail function, so no way to do it at the same time as you send the mail.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号