Solved

PHP forward to pipe

Posted on 2013-11-18
22
669 Views
Last Modified: 2013-11-20
Hi

I am trying to set up a forwarder to pipe to php script.

The entry in the forwarder list is as follow:

prakash@domain.com to |home5/nadlani1/public_html/parsing.php

The opening of the script is

#!/usr/bin/php -q
<?php

and the beginning of the bounce back message is:

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:

  pipe to |/home5/nadlani1/public_html/parsing.php
    generated by prakash@domain.com

The following text was generated during the delivery attempt:

------ pipe to |/home5/nadlani1/public_html/parsing.php
       generated by prakash@domain.com ------

Error in argument 1, char 3: option not found <
Usage: php [-q] [-h] [-s] [-v] [-i] [-f <file>]
       php <file> [args...]
  -a               Run interactively
  -b <address:port>|<port> Bind Path for external FASTCGI Server mode
  -C               Do not chdir to the script's directory
  -c <path>|<file> Look for php.ini file in this directory
  -n               No php.ini file will be used
  -d foo[=bar]     Define INI entry foo with value 'bar'
  -e               Generate extended information for debugger/profiler
  -f <file>        Parse <file>.  Implies `-q'
  -h               This help
  -i               PHP information
  -l               Syntax check only (lint)
  -m               Show compiled in modules
  -q               Quiet-mode.  Suppress HTTP Header output.
  -s               Display colour syntax highlighted source.
  -v               Version number
  -w               Display source with stripped comments and whitespace.
  -z <file>        Load Zend extension <file>.
  -T <count>       Measure execution time of script repeated <count> times.



If I remove the -q I simply get a bounce back with the following header:

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:

  pipe to |/home5/nadlani1/public_html/parsing.php
    generated by prakash@domain.com
    local delivery failed

Attached file is parsing.php file for reference.
parsing.php
0
Comment
Question by:Starquest321
  • 13
  • 5
  • 4
22 Comments
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 250 total points
Comment Utility
Are you trying to set up a dot-qmail file by any chance?
0
 

Author Comment

by:Starquest321
Comment Utility
No - not trying to set that up.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
Here's my teaching example.  In my experience you do not want the PIP script to produce anything that looks like browser output.  When my pipe produced browser output (even invisible white space) the mail server bounced the message.  That's why this script uses output buffering.  Hopefully the code and comments will be useful.
#!/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

0
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
Hmmm, not sure then. I would make sure you don't have any special characters. If I copy your script and test it locally, it works just fine.

Is that the ACTUAL final command:
pipe to |/home5/nadlani1/public_html/parsing.php
...or are you changing it for the purposes of posting here?
0
 

Author Comment

by:Starquest321
Comment Utility
Hi Gr8gonzo,

No special characters in script ..

pipe to |/home5/nadlani1/public_html/parsing.php is the actual path.
0
 

Author Comment

by:Starquest321
Comment Utility
My Script is straight forward ..

To check email piping is working or not ..

#!/usr/php/53/usr/bin/php -q
<?php
$final="It worked perfectly";
$fdw = fopen("/home5/nadlani1/public_html/bounce.txt", "w+");
fwrite($fdw, $final);
fclose($fdw);
?>

Using wring some text to text file to see file is executing properly or not..
0
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
Does it work when you execute it from the command line?

For example:

echo "Hello world" |/home5/nadlani1/public_html/parsing.php

Also, I'm noticing that your latest bit of code uses a different PHP path for the binary...
0
 

Author Comment

by:Starquest321
Comment Utility
Hi Gr8gonzo,

Path is fine no issues with path ::

Only problem with this first line ::

#!/usr/bin/php -q

Error in argument 1, char 3: option not found
Usage: php [-q] [-h] [-s] [-v] [-i] [-f <file>]
       php <file> [args...]
0
 

Author Comment

by:Starquest321
Comment Utility
Hi Ray_Paseur,

I tried your solution same result getting error ::

Error in argument 1, char 3: option not found
Usage: php [-q] [-h] [-s] [-v] [-i] [-f <file>]
       php <file> [args...]
0
 

Author Comment

by:Starquest321
Comment Utility
Hi Gr8gonzo,

I tried from Command Line :: This time my file is simple just echo statement ::

File is like this ::

#!/usr/bin/php -q
<?php
echo "Prakash";
?>

Used Command Line to see out put ::

echo "Hello world" |/home5/nadlani1/public_html/parsing.php

Same error ::

Error in argument 1, char 3: option not found
Usage: php [-q] [-h] [-s] [-v] [-i] [-f <file>]
       php <file> [args...]
  -a               Run interactively
  -b <address:port>|<port> Bind Path for external FASTCGI Server mode
  -C               Do not chdir to the script's directory
  -c <path>|<file> Look for php.ini file in this directory
  -n               No php.ini file will be used
  -d foo[=bar]     Define INI entry foo with value 'bar'
  -e               Generate extended information for debugger/profiler
  -f <file>        Parse <file>.  Implies `-q'
  -h               This help
  -i               PHP information
  -l               Syntax check only (lint)
  -m               Show compiled in modules
  -q               Quiet-mode.  Suppress HTTP Header output.
  -s               Display colour syntax highlighted source.
  -v               Version number
  -w               Display source with stripped comments and whitespace.
  -z <file>        Load Zend extension <file>.
  -T <count>       Measure execution time of script repeated <count> times.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
This seems to show that you have a closing PHP tag.  That is AntiPHPractice #13.  Try removing it - there may be invisible whitespace after it and that may be enough to cause the bounce.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:Starquest321
Comment Utility
Hi Ray_Paseur,

I tried the way u said .. But it is not working.

The thing is that It is not executing the script :: I think

Any other ways to check Script executing perfectly from Command Line Or not

I tried this way :: echo "Hello world" |/home5/nadlani1/public_html/parsing.php

getting same error.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Is this installed at a professional hosting company?  Do you have cPanel?

If you install the script I posted above (and make the obvious change on line 60) it will send you something when it gets executed.
0
 

Author Comment

by:Starquest321
Comment Utility
Hi Ray_Paseur,

I tried the way you said .. But similar error.
0
 

Author Comment

by:Starquest321
Comment Utility
Hi Ray_Paseur & Gr8gonzo,

Thanks for your support.

I fixed the issue :;

While adding Forwarders from Cpanel :: added Pipe like this
|/usr/bin/php -q /home5/nadlani1/public_html/parsing.php

"/usr/bin/php -q" part should match to the first line of parsing.php page .. Now it executed perfectly.

Thanks
0
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
Strange. It sounds like it was treating your PHP script as if it were a list of commands rather than a PHP script itself. But in most scenarios, that wouldn't happen - if you didn't have executable permissions on the file, then it would just throw an error about permissions, not try to run the file as if it were a bash script or something...

Glad it works now, though.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
I don't think you want to have this script in the web root (public_html) directory.  Just a thought...
0
 

Author Comment

by:Starquest321
Comment Utility
mmm...why?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
You would reduce the risk of accidental (unwanted) execution.  As long as you have a "dummy" script that does not change the data model, it's no problem.  But presumably some day the pipe script will be doing something meaningful -- perhaps storing data, or making data base updates.  If you do not have a public URL, you add a little better security.  Without the URL, the chances that the script was started by something other than an email are greatly reduced.  But if the script has a URL and someone discovers it, your applications becomes a target-rich environment for all kinds of mischief.
0
 

Author Comment

by:Starquest321
Comment Utility
I've requested that this question be closed as follows:

Accepted answer: 0 points for Starquest321's comment #a39657183
Assisted answer: 250 points for gr8gonzo's comment #a39656759
Assisted answer: 250 points for Ray_Paseur's comment #a39656791

for the following reason:

/
0
 

Author Comment

by:Starquest321
Comment Utility
I didn't do that. . . should be awarded equally to all members who answered.
0
 

Author Comment

by:Starquest321
Comment Utility
I didn't do that. . . should be awarded equally to all members who answered.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
This article discusses four methods for overlaying images in a container on a web page
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

771 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

10 Experts available now in Live!

Get 1:1 Help Now