View Full Version : Pipe bounced email!

09-09-2012, 08:15 PM
Well I was working on this all day yesterday and I finally got it to work!

Lets say you are sending out a newsletter or something to people in your DB. Chances are you are going to get some bounces. Now, why would you do nothing with these bounced emails? You should unsubscribe them from your DB or something! Here's how to do it:

Step 1: Make a php file and call it whatever you want.
Step 2: Paste the following code into it:

#!/usr/local/bin/php -q

$con = mysql_connect("localhost","xxxxxxxx","xxxxxxx");
if (!$con)
die('Could not connect: ' . mysql_error());

mysql_select_db("xxxxxxx", $con);

/* Read the message from STDIN */
$fd = fopen("php://stdin", "r");
$email = ""; // This will be the variable holding the data.
while (!feof($fd)) {
$email .= fread($fd, 1024);
/* Script start */

$start_limiter = '-0400
To: ';
$end_limiter = 'Subject: RapidCashGPT Newsletter';
$start_pos = strpos($email,$start_limiter);
$end_pos = strpos($email,$end_limiter,$start_pos);
$needle = substr($email, $start_pos+10, ($end_pos-11)-$start_pos);
//do action here

Step 3: Configure the following:

Change your database info on lines 4 & 10
On line 21/22, make sure that text appears in a bounced email. It may be different for everyone else
On line 23, change the subject to whatever the subject is of the outgoing email you sent
Code an action where it says //do action here. this could be a mysql query, or even writing it to a text document for later use

Step 4: In cpanel, go to "Account Level Filtering", click Create New Filter.
Name it whatever you like, then set up how you want them filtered. I set it up as "Subject Equals: Mail delivery failed: returning message to sender"

Step 5: Under Actions, click pipe to program, and put this:

/usr/bin/php -q /home/USERNAME/public_html/FILE.php
Be sure to change "username" to your account, and "FILE" to whatever you named the php file.

Step 6: Testing and fixing! There is only one problem with the code above, on line 26 you see 2 numerical values.. This is what deletes extra characters from the substring. When you change the start and ending limiters to fir your needs, the numbers will be off. Just play around with the numbers until you get no extra characters (or any accidentally deleted characters) at the end or beginning of the email!

Step 7: Save everything! It will now be working. Please note that "testing" the filter does not work correctly, and nothing will happen why you try to do so. If you really want to test it, send an email to your main account with the title filter you chose and the same body as a bounced email and see if it works! Also, there may be a better way to strip the email out of the email, so be my guest to change it and let me know! You might be able to pass a header when you're sending your emails to make your life a little easier.

Thanks would be appreciated :D Please let me know of any customization to this script that make it easier to use! :)

10-26-2012, 01:03 AM
Bumping this! :3

11-01-2012, 12:28 AM
It should be noted that this will only work on linux where email piping is available. On windows it is not possible to use piping.