0

I have setup my email forwarding to "Pipe to a program". The piping seems to be working because when an email is sent to my piped email address it returns an undelivered message with errors from my PHP script such as:

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 |/home/badihbar/public_html/handEmlTst.php generated by badih@badihbarakat.com local delivery failed

The following text was generated during the delivery attempt:

------ pipe to |/home/badihbar/public_html/handEmlTst.php generated by badih@badihbarakat.com ------

/home/badihbar/public_html/handEmlTst.php: line 1: ?php: No such file or directory /home/badihbar/public_html/handEmlTst.php: line 3: //: is a directory /home/badihbar/public_html/handEmlTst.php: line 4: //: is a directory /home/badihbar/public_html/handEmlTst.php: line 5: syntax error near unexpected token (' /home/badihbar/public_html/handEmlTst.php: line 5: // date_default_timezone_set("Asia/Muscat");'

------ This is a copy of the message, including all the headers. ------

Apparently, for some reason, it is not recognizing the php file to have php scripts. What I'm suspecting is the first line itself. How can I know what path should i put after the "#!" ?

My php script from handEmlTst.php:

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

// include_once "Libraries/connection.class.php";
// include_once "includes/readini.inc.php";
// date_default_timezone_set("Asia/Muscat");

$starttime = date("h:i:s");

// create the connection object...
$s = ini_get("mysql.default_host");

$conn = mysql_connect($s,'badihbar_badih','letmein2013');

$db = "badihbar_supportcenter";

$attachments = array();

if(mysql_error())
{
    print "Error Occured: ".mysql_error()."<br>";
    $err = true;
}

mysql_select_db($db,$conn);

$commString  = "INSERT INTO scriptEmails (Message) VALUES ('From the Script - ".$starttime."')";

// , '".mysql_real_escape_string($email)."'

$result = mysql_query($commString, $conn);
if(mysql_error())
{
    // echo "Error Occured Saving Data: ".mysql_error()."<br>";
    // print $Conn->commString."<br>";
}


?>

I have checked phpinfo() for the path but honestly didn't know what I'm looking for. So, I supposed that the php interpreter would be in usr/lib/ or something. Should I substitute usr with my domain user or something? I appreciate if anybody can advise in this regard.

many Thanks

Badih Barakat
  • 131
  • 1
  • 11
  • After adjusting the first line to #!usr/bin/php -q, I am getting the following in the bouncing undelivered mail message: – Badih Barakat Dec 12 '14 at 08:41
  • it will be usually /usr/bin/php try this `whereis php` or `which php` to find out the path – Arun Kumar Dec 12 '14 at 08:42
  • Error in argument 1, char 3: option not found Usage: php [-q] [-h] [-s] [-v] [-i] [-f ] php [args...] -a Run interactively -b | Bind Path for external FASTCGI Server mode -C Do not chdir to the script's directory -c | 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' – Badih Barakat Dec 12 '14 at 08:43
  • dear Arun, usr/bin/php seems to be correct... but it is giving some argument error... check my second comment for the first few lines of the returned error... – Badih Barakat Dec 12 '14 at 08:44
  • remove the argument and try just `#!/usr/bin/php` – Arun Kumar Dec 12 '14 at 08:46
  • ------ pipe to |/home/badihbar/public_html/handEmlTst.php generated by badih@badihbarakat.com ------ Could not exec '/home/badihbar/public_html/handEmlTst.php' ------ This is a copy of the message, including all the headers. ------ – Badih Barakat Dec 12 '14 at 08:47
  • Thats what I got. I couldn't execute the script... – Badih Barakat Dec 12 '14 at 08:47
  • have you given execute permissions for the script `chmod 755` – Arun Kumar Dec 12 '14 at 08:49
  • Yes, it is chmod 755 since the beginning... – Badih Barakat Dec 12 '14 at 08:51
  • try this [Piping](http://stackoverflow.com/questions/24901079/implementing-a-secret-email-feature-that-inserts-content-into-database/24901828#24901828) – Arun Kumar Dec 12 '14 at 08:54
  • Still didn't help... – Badih Barakat Dec 12 '14 at 11:18

2 Answers2

4

I managed to get it working after all. Below are the steps for all to benefit... :)

  1. I used phpinfo() to get the path for the php interpreter. I used the "Configuration File (php.ini) Path" parameter value which was usr/lib/ for my server.
  2. used #!/usr/lib/php -q as the first most line in my handling script.
  3. This part was the issue in my case. UPLOAD THE FILE AS ASCII, NOT BINARY!
  4. Made sure the chmod of the uploaded file was 755.
  5. Set the forwarding of my email address to "Pipe to a program" in my cpanel and put the path of the php script file I've uploaded.

That's did it. As I mentioned, my problem was with the file being uploaded by the FTP application as binary instead of ASCII.

Thanks, all... :)

Badih Barakat
  • 131
  • 1
  • 11
-1

You can also use #!/usr/local/bin/php at the starting line on your PHP file, which is the cli binary instead of the cgi binary.

This works for me, If you Passed above case as said by @Badih Barakat

WhiteHorse
  • 718
  • 2
  • 8
  • 15
  • The precise path to use in the shebang obviously depends on the installation. On many distros, using the package manager will install PHP in `/usr/bin` and there will be pretty much nothing in `/usr/local/bin`. If you (or your admin) installed PHP from source, `/usr/local/bin` is the likely preferred location (though you also see `/opt` and what not). – tripleee Sep 07 '15 at 05:52