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 */
|
Add to Favourites
Print this Article
|