Link to home
Start Free TrialLog in
Avatar of sasnaktiv
sasnaktivFlag for United States of America

asked on

Ray Paseur's email bounce back php script functions well, but I need help with some tweaking.

Hi Guys,
I've been using an e-mail bounce back script that Ray Paseur wrote and it's been functioning well for me. I'm very grateful to him for sharing it.

I just need help with some tweaking. I need to capture the headers or the area of the message where the bounce back errors are listed when the bounced email is returned to me.

I can get that info from two or Ray's variables $buf and $raw_email, BUT that also gives me the entire contents of the original email which I do not want.

So, what can be done (similar to $buf) which will isolate the returned message without including the contents of the original?
Thanks,
Sas


#!/usr/bin/php -q
<?php
/**
 * /email_pipe/index.php
 * DATED: 2009-05-12
 *
 * 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 = NULL;

// TRY TO READ THE EMAIL FROM STDIN
if (!$stdin = fopen("php://stdin", "R"))
{
    echo 'ERROR: UNABLE TO OPEN php://stdin' . PHP_EOL;
}

// 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);

// PREVENT ANY BROWSER OUTPUT - MAY CAUSE ERROR RESPONSES AND BOUNCED MESSAGES
ob_end_clean();

Open in new window

Avatar of hielo
hielo
Flag of Wallis and Futuna image

On line 54 try pasting these:

$temp = preg_split("#\r?\n\r?\n#",$raw_email,1);
var_dump($temp); // $temp[0] should have the headers only

Open in new window

Avatar of sasnaktiv

ASKER

Thanks for the quick response Hielo.
I'll give it a try and get back to you.
Sas
Hi Hielo,
All I get is the word "Array" when the results are emailed to me.
$temp = preg_split("#\r?\n\r?\n#",$raw_email,1);
//var_dump($temp); // $temp[0] should have the headers only //

mail ('sas@xxxx.com', 'From xxxx.com/BouncedMAIL/RaysBounceReport.php',$temp); 

Open in new window

However Hielo, I do get the results I want when I use the following code. The thing is that it may not be all that reliable since I'm counting on every bounced message to contain the text (This is a copy of the message, including all the headers) and I don't think that's reality.
$BUF_errors = substr($raw_email, 0, strpos( $raw_email, 'This is a copy of the message, including all the headers')); 
// Above deletes everything after 'This is a copy of the message, including all the headers'

mail ('sas@xxxx.com', 'From xxxx.com/BouncedMAIL/RaysBounceReport.php',$BUF_errors); 

Open in new window


Any advice?
You need to email $temp[0]:
mail ('sas@xxxx.com', 'From xxxx.com/BouncedMAIL/RaysBounceReport.php',$temp[0]); 

Open in new window

Even when I include $temp[0] as suggested Hielo, I still get the full email -- both headers and content.

mail ('sas@xxxx.com', 'From xxxx.com/BouncedMAIL/RaysBounceReport.php',$temp[0]); 

Open in new window

Please show us the complete contents of the message you receive from this instruction at the end of the script.
mail ('you@your.org', 'EMAIL PIPE DATA', $buf);

Open in new window

The goal here is to look at the string in $buf and identify the breakpoints so that we can (1) identify the bounce messages and (2) cut off the irrelevant part of the text string.
Hi Guys,
It would be useless & impractical to show you the complete contents of the message from the $buf simply because there are hundreds of them and each is unique.
"(1) identify the bounce messages " is executed long before it is presented to Ray's original script. So I'm only looking to "(2) cut off the irrelevant part of the text string."

Hielo's solution (even using $temp[0] ) consistently gives me the complete email contents. Is something off in the code Hielo provided?
$temp = preg_split("#\r?\n\r?\n#",$raw_email,1);
//var_dump($temp); // $temp[0] should have the headers only //

Open in new window


My original approach (code below) functions flawlessly, but I'm concerned that the text (This is a copy of the message, including all the headers) may not be an industry standard and therefore may not included in every bounce message from every mail server on every planet in every galaxy.
That's why I'm looking for a reliable alternative.
$BUF_errors = substr($raw_email, 0, strpos( $raw_email, 'This is a copy of the message, including all the headers')); 
// Above deletes everything after 'This is a copy of the message, including all the headers'

mail ('sas@xxxx.com', 'From xxxx.com/BouncedMAIL/RaysBounceReport.php',$BUF_errors); 

Open in new window

Does such an alternative exist?
On my first post, change the last argument from 1 to 2:
$temp = preg_split("#\r?\n\r?\n#", $raw_email, 2);

and email $temp[0]
Please show us the complete contents of the message...
It would be useless & impractical to show you the complete contents of the message...
Suit yourself, I'm not going to waste my time guessing.  I'm out.  Good luck with the project.
Thanks for sticking with me Hielo,
Your help is much appreciated. Please excuse the way I'm presenting this information. When I use bold it's for clarity. I'm not shouting.
Sas

$temp = preg_split("#\r?\n\r?\n#",$raw_email,1);
gives me the complete email content.


$temp = preg_split("#\r?\n\r?\n#",$raw_email,2);
gives me the following.
But it cuts the headers off too soon, deleting the part of the header that presents the data I need.


>From MAILER-DAEMON Fri Jul 03 16:17:30 2015
Received: from mailnull by 1234.VPSserver.com with local (Exim 4.85)
      id 1ZB7Os-0000dU-1q
      for ccccc_@domain.com; Fri, 03 Jul 2015 16:17:30 -0400
X-Failed-Recipients: Recipient_1@destinationEMAIL.com
Auto-Submitted: auto-replied
From: Mail Delivery System <Mailer-Daemon@1234.VPSserver.com>
To: ccccc_@domain.com
Subject: Mail delivery failed: returning message to sender
Message-Id: <E1ZKKG7Os-0000dU-1q@1234.VPSserver.com>
Date: Fri, 03 Jul 2015 16:17:30 -0400


Below is a sample of the data that I get with my original version.
Notice how much more data is presented.


string(14689) "From MAILER-DAEMON Fri Jul 03 15:02:27 2015
Received: from mailnull by 1234.VPSserver.com with local (Exim 4.85)
      id 1ZB6EE-0000Iv-SC
      for ccccc_@domain.com; Fri, 03 Jul 2015 15:02:26 -0400
X-Failed-Recipients: Recipient_2@destinationEMAIL.com
Auto-Submitted: auto-replied
From: Mail Delivery System <Mailer-Daemon@1234.VPSserver.com>
To: ccccc_@domain.com
Subject: Mail delivery failed: returning message to sender
Message-Id: <E1ZBE-0000Iv-SC@1234.VPSserver.com>
Date: Fri, 03 Jul 2015 15:02:26 -0400

This message was created automatically by mail delivery software.

A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:

 Recipient_2@localSERVICE.com
 host inbound.localSERVICE.com [207.251.194.26]
 SMTP error from remote mail server after RCPT TO:<Recipient_2@localSERVICE.com>:
 550 5.1.1 <Recipient_2@localSERVICE.com>: Recipient address rejected:
 User unknown in relay recipient table

------ This is a copy of the message, including all the headers.
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Hielo,
That makes a lot of sense to me.
Give me some time to kick the tires on it and I'll get back to you with the results.
Sas
You nailed it Hielo!
Thanks for your patience & understanding. You've been a great help.
Sas