Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bounce Email handling with PHP?

The best scenario is be able to classify the type of bounce: soft, hard...

what we use is BounceStudio. You need to compile it and add the php libraries... not hard at all. You have the free and paid version of that product

once we detect the kind of bounce we use PEAR::MAIL::MIME to search for custom headers that we added previously to the email, lets say:

X-user-id: XXXXX
X-campaign-id: YYYYYY 
X-recipient-id: SSSSSSSSS

in this way we can know the real recipient that we sent the email to.

hope this help you! so you can help me to get to the 500 points :P


Why not create a [email protected] and use php to read those emails and do what ever you want?

Edit After your comment : Please chec my link whcih has a php script which will teach you how to open and email box using php and read the emails. You can use this scrip to check the error messages.


Let the emails bounce to an address that is really an emailadress (with login details etc.).

Make a php script which runs ever x minutes (for example with a cron job). This php script must do the following. - Retrieve all email from the box (use for example Zend Mail) - Check for the error in the message (e.g. by searching it with regular expressions) - Do what ever is necessary.

If you want to know specifically who has bounced back you can use user specific bounce addresses. (See for example this site)


Maybe it's a little late for the answer, but you can always try something new. I had the last week a task like this, and used BOUNCE HANDLER Class, by Chris Fortune, which chops up the bounce into associative arrays - http://www.phpclasses.org/browse/file/11665.html

This will be used after you connect to the POP3 with some mailer to get the bounces from it, then parse it into pieces with this, and if has the status you searched for, do the necessary actions.

Cheers.


If you've got a POP3 mailbox set up for [email protected], you could use a POP3 client script written in PHP to retrieve the messages and check for undeliverable messages.


You can use imap_open to access your mails from PHP.

This functions also works for POP3 but not every function may work here. However I guess in 2018 most email-clients should support IMAP.

This function can also be used to open streams to POP3 and NNTP servers, but some functions and features are only available on IMAP servers.

Here is a little example, how to iterate through your emails:

  /* connect to server */
  $hostname = "{$your-server:$your-port}INBOX";
  $username = 'my-username';
  $password = '123';

  /* try to connect */
  $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to mailbox: ' . imap_last_error());

  /* grab emails */
  $emails = imap_search($inbox,'ALL');

  /* if emails are returned, cycle through each... */
  if($emails) {
    /* for every email... */
    foreach($emails as $email_number) {

        $message = imap_body($inbox,$email_number,2);
        $head    = imap_headerinfo($inbox, $email_number,2);
        // Here you can handle your emails
        // ...
        //  ...
      }
  }

In my case, I know that I always get my mail delivery failed from [email protected]. So I could identify bounces like that:

if($head->from[0]->mailbox == 'Mailer-Daemon')
{
  // We have a bounce mail here!
}

You said:

When, the email can't be sent, it's sent to [email protected], the error message could be 553 (non existent email ...) etc.

So if your bounce emails have the subject "Mail delivery failed: Error 553" then you could identify them by the subject like this:

if($head->subject == 'Mail delivery failed: Error 553')
{
  // We have a bounce mail here!
}

The failed email address is not in the header, so you need to parse it from the $message variable with some smart code.