?
Solved

How to parse email bounce backs in PHP

Posted on 2010-11-22
6
Medium Priority
?
476 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 36

Accepted Solution

by:
Loganathan Natarajan earned 2000 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 111

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

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

762 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