Solved

How to parse email bounce backs in PHP

Posted on 2010-11-22
6
467 Views
Last Modified: 2013-12-12
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
Comment
Question by:skione
  • 2
  • 2
6 Comments
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 34194235
0
 
LVL 36

Accepted Solution

by:
Loganathan Natarajan earned 500 total points
ID: 34194278
0
 

Author Comment

by:skione
ID: 34195911
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34195993
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
 

Author Comment

by:skione
ID: 34197665
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP delete contents of file- before writing to it 6 49
Add a loading gif while php runs server side 15 63
MySQL-Design Help 12 38
PHP $_POST vs asp request 4 19
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question