Navruk
Navruk

Reputation: 927

Handling mail failures using PHP?

If I send an email through GMail to this address [email protected]

I got error like:

Delivery to the following recipient 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 <[email protected]>, Kelly <[email protected]>' . "\r\n";
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";

$to = "[email protected]";
$subject = "Testing";
$message = "Testing body";

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

Upvotes: 2

Views: 1378

Answers (6)

BvdVen
BvdVen

Reputation: 2961

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

$bounce = '[email protected]';
mail($to, $subject, $message, $headers,"-f $bounce");

This will send all bounce back mails to your address

Upvotes: 2

Alix Axel
Alix Axel

Reputation: 154691

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

Upvotes: 1

Spudley
Spudley

Reputation: 168853

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.

Upvotes: 1

powtac
powtac

Reputation: 41080

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.

Upvotes: 1

CAFxX
CAFxX

Reputation: 30351

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.

Upvotes: 2

Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28583

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.

Upvotes: 0

Related Questions