How to Pipe / Send an Email to a PHP Script

Posted on

Sending an email to a PHP script (also called pipe), is a fairly simple task to accomplish. You first need to setup a PHP file and then you will setup pipe forwarding in cPanel.  This tutorial is only the basics and is using cPanel to setup the actual pipe. Alright let us get started with the PHP file.

  1. The very first line of the PHP script is a hashbang (also called shebang). This is a special line which identifies the file as a PHP script and specifies the path to the PHP binary. In most cases it should look like this:
    1
    
    #!/usr/bin/php –q

    But on some occasions this just won’t work.  If it doesn’t you either have to change the path to the PHP binary or you need to use the following:

    1
    
    #!/usr/bin/php-cli –q

    What this does is runs the PHP binary from the Command Line (cli = command line interface).

    Notes: 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.

  2. Now that we have the first line done, we need to get the emails content.  This is accomplished using the following code below:
    1
    
    file_get_contents('php://stdin');

    The script itself will grab the emails content using file_get_contents on php://stdin which is where the email is temporarily stored. You can then manipulate it using such functions as preg_match to grab the parts you want.

  3. Now to put entire the PHP script together.  See the code below:
    1
    2
    
    #!/usr/bin/php –q
    <!--?php  $emailContent = file_get_contents("php://stdin"); mail('email@yoursite.com','Email Pipe Works!',  $emailContent); ?-->

    As you can tell this is just a very simple script that gets the email contents and just sends it back to you.  It just lets you know if it was successful.

  4. Now upload the PHP file to your site and 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.
    1. Now on to setting up the pipe forwarding (sending an email to our PHP script), follow the steps below:
    2. Log in to your cPanel.
    3. Click on the forwardersForwarders” icon, under the “Mail” tab.
    4. Click on the “Add Forwarder” button.
    5. Fill in “Address to Forward” and put the mail address you would like to pipe the messages from.
    6. Select “Pipe to a Program” and fill in the full path to the PHP script which will handle the messages.  Luckily in cPanel 11 makes this easy because it automatically puts you in your home directory. So all you need to do is just add a relative path. Click here to see the cPanel image.

I hope that this tutorial has helped you.  If you have any questions please comment below.

Tags: , ,