• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 493
  • Last Modified:

How to parse email bounce backs in PHP

I have a messaging app that, among other things, sends emails. I would like to be able to parse the emails that bounce and remove them from my list. I already have code that can check and download emails from an IMAP account (for unsubscribes) but I would also like to parse the errors that get sent to the main account. Is there any guide for how to do this?
0
skione
Asked:
skione
  • 2
  • 2
1 Solution
 
skioneAuthor Commented:
Since I made this post I actually wrote a routine to do this, so procmail is not a solution for me. I am using PEAR Authenticated SMTP to send emails. So bounce backs end up in the Reply to address. The routine I wrote reads the emails via IMAP and then looks for anything that looks like an email address, registers a bounce and attempts to remove them from the list of subscribers. However this is far from perfect. Even things like "Out of Office" replies will register as a bounce. Is there a parsing database out there with much of this already done or do I have to write the hole thing myself playing "cat and mouse" along the way?
0
 
Ray PaseurCommented:
You can use an email "pipe" to receive the messages.  Sorry, but unless you already know exactly what email bounces you want to process, and how you want to process them, there is a bit of a cat-and-mouse operation to enable you to decide.  You might want to start by searching Google for "email 550" since that is one of the rejection headers.

A good way to handle this process would be to use the PIPE script to capture the email messages and send them on to you in plain text.  Then you can inspect the headers and content and decide which ones represent signal and which are noise.  Here is an example of a PIPE that works for me.

Good luck with it, ~Ray
#!/usr/bin/php -q
<?php // /email_pipe/index.php


// THIS IS AN EMAIL PIPE SCRIPT.
// THIS SCRIPT IS STARTED AUTOMATICALLY FOR EACH MESSAGE.
// NOTE THAT THIS SCRIPT IS ABOVE THE /public_html/ DIRECTORY TO PREVENT ACCIDENTAL EXECUTION


// --> HOW DO WE KNOW WHICH EMAIL MESSAGES GET SENT HERE?
// THIS SCRIPT RECEIVES MESSAGES SENT TO email_pipe@your.org
// CREATE AN EMAIL MAILBOX EXCLUSIVELY FOR AUTOMATED PROCESSING.
// SET UP AN EMAIL FORWARD FOR THAT MAILBOX IN cPANEL->EMAIL LIKE THIS:
// 1...5...10...15...20...25...
// |/home/{account}/email_pipe/index.php


// --> WHEN YOU UPLOAD, THIS SCRIPT WILL BE MARKED RW-R-R BUT THAT IS WRONG
// THIS SCRIPT MUST BE MARKED EXECUTABLE x0755
// YOU CAN USE FTP SOFTWARE TO CHMOD TO RWX-RX-RX


// --> NOTE THE FIRST LINE OF THIS SCRIPT MUST SAY #!/usr/bin/php -q STARTING IN COLUMN ONE
// 1...5...10...15...20...25...
// #!/usr/bin/php -q
// <?php ... PROGRAM CODE FOLLOWS


error_reporting(E_ALL);

// USE THE OUTPUT BUFFER - THIS DOES NOT HAVE BROWSER OUTPUT
ob_start();

// COLLECT THE INFORMATION HERE
$raw_email = '';


// TRY TO READ THE EMAIL FROM STDIN
if (!$stdin = fopen("php://stdin", "R"))
{
    echo "ERROR: UNABLE TO OPEN php://stdin \n";
}

// ABLE TO READ THE MAIL
else
{
    while (!feof($stdin))
    {
        $raw_email .= fread($stdin, 4096);
    }
    fclose($stdin);
}


// REMOVE MULTIPLE BLANKS - AND OTHER PROCESSING AS MIGHT BE NEEDED
$raw_email = preg_replace('# +#', ' ', $raw_email);

// SPEW WHAT WE GOT, IF ANYTHING, INTO THE OUTPUT BUFFER
var_dump($raw_email);

// CAPTURE THE OUTPUT BUFFER AND SEND IT TO SOMEONE ELSE VIA EMAIL
$buf = ob_get_contents();
mail ('you@your.org', 'EMAIL PIPE DATA', $buf);
ob_end_clean();

Open in new window

0
 
skioneAuthor Commented:
Yeah procmail nor pipe will work for me as I am opening mailboxes on remote hosts.

But I think you have answered my question none the less.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now