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

LVL 1
sasnaktivAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
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

0
sasnaktivAuthor Commented:
Thanks for the quick response Hielo.
I'll give it a try and get back to you.
Sas
0
sasnaktivAuthor Commented:
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?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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

Open in new window

0
sasnaktivAuthor Commented:
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

0
Ray PaseurCommented:
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.
0
sasnaktivAuthor Commented:
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?
0
hieloCommented:
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]
0
Ray PaseurCommented:
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.
0
sasnaktivAuthor Commented:
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.
0
hieloCommented:
OK, but this boils down to breaking the text at some specific delimiter.  In your first attempt, you chose:
"This is a copy of the message, including all the headers.", which is why it was working for you.

But if you look closer, that line is actually:
------ This is a copy of the message, including all the headers.

Other similar lines that you are likely to encounter are:
----- Transcript of session follows -----
----- The following addresses had permanent fatal errors -----

The common denominator is that they start with consecutive hyphens.  Thus, I propose you try:
$temp = preg_split("#----.+#", $raw_email, 2);

Open in new window


and email $temp[0]
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sasnaktivAuthor Commented:
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
0
sasnaktivAuthor Commented:
You nailed it Hielo!
Thanks for your patience & understanding. You've been a great help.
Sas
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.