Helpdesk site specific to Bluehost | Hostmonster | Fastdomain
Header

Piping email to a program

November 26th, 2011 | Posted by admin in Uncategorized

There are several important things you should check regarding the PHP script handling the mails:

Ensure the very first line of the script is a hashbang (also called shebang). This is a special line which identifies the file as a PHP script. In most cases it should look like this:

#!/usr/bin/php -q

Make sure that there are no whitespaces or blank lines before the above line as this will be sent to the mail server, which will result in a bounced message. The -q option instructs PHP not to print its version either, since this will also result in a bounced message.

Make sure that the script permissions are set correctly. In most cases, you would simply need to change the permissions, either via your cPanel FileManager or an FTP client and set them to 755. This will make the script executable.

Check that your script has NO closing PHP tag (?>) in order to prevent any output being sent to the mail server.

You can find below a sample PHP script which handles piped messages. The script will read the input from the STDIN and then save the full message into a file called mail.txt in the same folder where it resides.


#!/usr/bin/php =q
/* Read the message from STDIN */
$fd = fopen("php://stdin", "r");
$email = ""; // This will be the variable holding the data.
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
/* Saves the data into a file */
$fdw = fopen("mail.txt", "w+");
fwrite($fdw, $email);
fclose($fdw);
/* Script End */

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>