Solved

PHP forward to pipe

Posted on 2013-11-18
22
706 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
[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
  • 13
  • 5
  • 4
22 Comments
 
LVL 35

Accepted Solution

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

Author Comment

by:Starquest321
ID: 39656774
No - not trying to set that up.
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 39656791
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
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 
LVL 35

Expert Comment

by:gr8gonzo
ID: 39656802
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
ID: 39656841
Hi Gr8gonzo,

No special characters in script ..

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

Author Comment

by:Starquest321
ID: 39656848
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 35

Expert Comment

by:gr8gonzo
ID: 39656857
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
ID: 39656880
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
ID: 39656885
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
ID: 39656907
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 110

Expert Comment

by:Ray Paseur
ID: 39656962
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
 

Author Comment

by:Starquest321
ID: 39656978
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 110

Expert Comment

by:Ray Paseur
ID: 39657000
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
ID: 39657020
Hi Ray_Paseur,

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

Author Comment

by:Starquest321
ID: 39657065
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 35

Expert Comment

by:gr8gonzo
ID: 39657088
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 110

Expert Comment

by:Ray Paseur
ID: 39657174
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
ID: 39657183
mmm...why?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39657278
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
ID: 39661514
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
ID: 39661511
I didn't do that. . . should be awarded equally to all members who answered.
0
 

Author Comment

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

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

734 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