E-mail PIPEs are e-mail forwarders that forward an email to an application and not an other email address. Recently I had to develop such a script so I thought I could share what I learned with you.

1.) Setting up the PIPE
Firstly you have to make sure your web hosting provider allows you to create E-mail forwarding PIPEs. Most cPanel accounts have pipes enabled.
Go to you cPanel account and click on ‘Forwarders’  and the click on ‘Add Forwarder’ button.

cPanel emai forwarder setup

After this the following screen will appear:

In this screen select a forwarder email address, then click on ‘Advanced options’ and select the address of the PHP script that will process the email.
* do not enter the address of some processing software, like: php, /usr/local/bin
* the address has to be relative to the document root

2.) Creating the processing file
After the PIPE is set up it’s time to create the script that will process the e-mail.

In my example above I used an ‘email.php’ named PHP script, located in public_html(check the screenshot). So, create your php file.

The first line of the script has to be the interpreter of your script, in this case PHP.
Example: #!/usr/local/bin/php -q
It has to start with #! and has to be followed by the full path to your PHP script. If you don’t know the path to it ask your webhosting company.
You can find more about hashbangs here:

When you upload the file you have to pay attention to the following matters:
* File access has to be at least 755 (execution permissions)
* Make sure that your FTP Client uses ASCII transfer type and not binary (the PIPE is very sensitive, it will throw errors if you upload a file created on Windows to a Unix server in binary mode, the ASCII mode will convert the new line symbols)
* Make sure you will not have any output, any output will be interpreted as an error in forwarding, even a blank space!!! (so use error_reporting(0) and ob_start() – ob_end_clean() functions to make sure you won’t have any output). The line after the hashbang should start with <?php do not leave other lines empty!

If you payed attention to the 3 lines above it will save you a lot of time while debugging.

3.) The script itself

The PIPE will send the email to the STANDARD INPUT (stdin).
So let’s use this code to get the e-mail:

// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
$email .= fread($fd, 1024);

Now, the $email variable will contain the entire email, who sent it, text, subject, attachement, headers, etc…

Now you’ve got everything you need, you just have to parse the email and get the data you need.

Read the article about decoding emails