Solved

How to parse email bounce backs in PHP

Posted on 2010-11-22
6
468 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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
curl parse data from site 20 61
How do I speed up this PDO query 4 37
Help installing Laravel app on MAMP on MAC 7 51
Undefined variable with $_POST in PHP 5 32
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
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.

738 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