bspacer1 bspacer2
webmail hosting forum clients about

Computer Co-op, CornerNET Internet Gateway, and PJR Solutions is owned by PJR Sales & Service. Quality products and online services since 1991. We can be your
Complete Computer Connection

Hosting @ $5.95

Domains @ $16.98
Home PagePortal HomeClient AreaAnnouncementsKnowledgebaseSupport TicketsDownloadsRegister

Knowledgebase
You are here: Portal Home > Knowledgebase > cPanel Related Issues > cPanel HOW-TOs > How to pipe an email to a PHP script

How to pipe an email to a PHP script

In some cases you might need to redirect a given mailbox to a PHP script to handle the incoming messages. This can be easily done with cPanel.

To set up a pipe forwarding, follow the steps below:

Log in to your cPanel.

Click on the `Forwarders` icon, under the `Mail` tab.

Click on the `Add Forwarder` button.

Fill in `Address to Forward` and put the mail address you would like to pipe the messages from.

Select `Pipe to a Program` and fill in the full path to the script which will handle the messages.

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 */



Was this answer helpful?

Add to Favourites
Print this Article

Also Read
How can I create different types of web redirect with my cPanel? (Views: 9264)
How can I enable anonymous FTP access in cPanel? (Views: 10288)
How can I retrieve the raw Apache access logs for my site? (Views: 2110)
How can I check my quota and bandwidth limits/usage? (Views: 11938)
How can I password protect a directory with cPanel? (Views: 11188)

Powered by WHMCompleteSolution


Copyright © 1994-2021 PJR Sales and Service All Rights Reserved