Solved

How to parse email bounce backs in PHP

Posted on 2010-11-22
6
458 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
Comment Utility
0
 
LVL 36

Accepted Solution

by:
Loganathan Natarajan earned 500 total points
Comment Utility
0
 

Author Comment

by:skione
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now