Pipe / Send Email to a PHP Script

Sending (or piping) emails to a php script would allows a whole world of fun. I had a spare 30 minutes the other night so I sat down, read a few blog posts and forums and set up emails to pipe to a php script.

The first step for me was to set up a new subdomain in cPanel (a control panel my affordable host includes). This allows me to only send (or pipe) specific email to the script and means I can run a catch-all wildcard forwarder on all emails to that subdomain.

In cPanel I created the subdomain in the normal way. Then I navigated to ‘Email Management Tools -> Default E-mail account’, chose the new subdomain from the dropdown list, clicked ‘Advanced Options’ and selected the radio button for ‘Pipe to a Program’.
In the textbox I added the name of the file that would be used to process the emails. In my case catcher.php which I had already created in the root of my site (not in public_html).

Cpanel is clever enough to add your sites root folder to the name you specify, so will be altered to something like ‘/home/youraccount/catcher.php’

The next step was to prepare catcher.php to deal with the emails being piped to it. Open the file and change the contents to something along the lines of:

#!/usr/bin/php -q
<?php
 
// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd))
{
	$email .= fread($fd, 1024);
}
fclose($fd);
 
 
mail('you@yoursite.com','From my email pipe!','"' . $email . '"');
 
?>

The first line of the script is very important and will tell the email pipe to use php. If your php install is not found at the location show, change it so it is. The -q part tells the pipe not to bounce an email back to the sender. A good thing as we can do that manually in the script.

Make sure you change the permissions of the script to be executable by the email pipe. chmod 755 should do fine.

The script itself will grab the emails contents using fopen 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.

The beauty of this method is it will catch all email to that subdomain, so you can create addresses which include the users id and a hash which can be temporary:

1294710241-w98fqwfhi3ho2ih3f@emailin.mysite.com

which you can use to ensure you only act on emails from the intended user.

Note: If you are using -q but still getting bounces, be sure the script is not outputting any data. That means once you have tested it, no print, no echo, no var_dump. When you run the script directly you should see a blank screen and the source code should be empty.

16 comments.

  1. nice up to date post – any idea why i’m still receiving bounces though? i’m using the -q but still getting them

  2. Do you have any more information G?
    The -q should suppress output from the script and therefore ensure no error, and therefore no bounce.

  3. [...] suppose you could use email alerts in combination with a Pipe Email to PHP Script to make them more flexible, but you are still only getting limited information to act on, and [...]

  4. Hey, did you solve that bounce problem? I have the same issue.

  5. Hi Zac,

    As mentioned above. Using -q on the first line of the script should suppress any script output and therefore any bounce. If you are still seeing bounces then you may need to speak to your server team about how to stop them.

    Harry

  6. I’m having a little problem with mine. Every time I try to send a message to the script, it comes back with an error, saying that “local delivery failed”. Any suggestions?

  7. Nevermind, I got it! Great post though!!

  8. This works perfectly. Thank you.

    I need to take this a couple steps further… maybe you can point me in the right direction:

    1. I need to save an XML file that gets attached to each inbound email so I can parse it and updated my database…

    2. I need to parse the email body to pick out elements that I will be putting into another table in the database…

    What is the best class or method to do this and do you have any examples?

    Your piping example is exactly what I needed to get started here.

  9. this is again emailing I would like to save values to database

  10. @Matt: Could you please share how you solved the problem?

  11. Any suggestion on how to use email piping without cPanel? I am at GoDaddy and they do not have cPanel.

  12. Hi Guys,

    This is exactly what I’m after, however I cannot get the script to work. Any test emails I send get bounced back. A quick question:

    How do you determine the exact location of the php install directory (as referred to in line1 of the script). I can then verify it’s correct for my server.

    Thanks.

  13. I had the same problem as Matt, i.e. I was getting “local delivery failed” responses. I set the permissions of the file to 755 as mentioned in this article and all’s well!

  14. Hi Andy,

    Create a php file with just the function phpinfo() in it:

    < ?php

    phpinfo();

    ?>

    The php install location is often listed in there.

  15. Hi Joseph,

    Not sure I’m afraid. cPanel takes care of the tough part so how you would do it without a powerful control panel I have no idea.

  16. if you do the above and your script isn’t working, don’t forget to save your php script in unix format, that is removing Linefeeds or maybe its returns with dos2unix.

Post a comment.