Dec 1, 2017

Pipe an email to a script

Piping an email to a script can be easily done with cPanel -> Forwarders tool.

To set up pipe forwarding, you can follow the steps below:

- Log in to your cPanel;

- Click on the Forwarders icon, under the Mail section;

- Click on the Add Forwarder button;

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

- Click on the Advanced options link and select Pipe to a Program. Then fill in the full path to the script which will handle the messages. Bear in mind that the path is relative to your home directory, so if you create a script/file called placed inside your home/ folder you should fill in only inside the Pipe to a Program field.

There are several important things you should check regarding the script which is handling the email messages:

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 script.

For example, in PHP, it could look like this:
#!/usr/local/php70/bin/php-cli -q

Make sure that there are no white spaces 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. You need to make the script executable.