Pipe email to a php script

Based on an old article written in 2002 at evolt.org that explains how to pipe an email to a php script.

This should work with any php CGI build

The below script will process the email and break it into several variables you can use in your own operations. They are:

  • $from
  • $subject
  • $headers
  • $message

1.Copy the below code into a php file emailprocess.php upload to your web server.

2.Create a PIPE to the script (available in CPANEL etc)

#!/usr/bin/php -q
<?php
// read from stdin
$fd = fopen(“php://stdin”, “r”);
$email = “”;
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
// handle email
$lines = explode(“\n”, $email);
// empty vars
$from = “”;
$subject = “”;
$headers = “”;
$message = “”;
$splittingheaders = true;

for ($i=0; $i < count($lines); $i++) {
if ($splittingheaders) {
// this is a header
$headers .= $lines[$i].”\n”;
// look out for special headers
if (preg_match(“/^Subject: (.*)/”, $lines[$i], $matches)) {
$subject = $matches[1];
}
if (preg_match(“/^From: (.*)/”, $lines[$i], $matches)) {
$from = $matches[1];
}
} else {
// not a header, but message
$message .= $lines[$i].”\n”;
}
if (trim($lines[$i])==””) {
// empty line, header section has ended
$splittingheaders = false;
}
}
?>

NOTE: If you find you are getting bounce messages (even though the script worked) you will find this is caused by the script outputting some data. Ensure you are not echoing data etc. remember this script is designed to be call internally by the mail process not via the browser, so requires no output!

I have used the above script in an email to sms utility i have created, which can also be found on this website.

Based on http://www.evolt.org/article/Incoming_Mail_and_PHP/18/27914

Rating: 5.00/5. From 4 votes.
Please wait...

4 thoughts on “Pipe email to a php script”

  1. all good things

    No votes yet.
    Please wait...
  2. 1. Go to Cpanel and in setting a Catch-all email account,choose option to pipe mail to script, enter path in the following way:

    public_html/myfolder/pipescript.php

    2. Go to your parseall script and do what you want with the content of the mail). with this at the very top

    [code]
    #!/usr/local/bin/php -q

    [/code]

    And you’re good to go.

    Note: Make sure there is no print or echo statement in the script, no output or warnings otherwise the sender of the email will receive an error message like the mail wasn’t received.

    Thank you Jesus for this.
    3. After parsing email, on your XP computer, remove all linefeeds and line returns with Dos2unix by going to cmd and inputting:

    dos2unix pipescript.php

    4. Upload the unixfied file.

    5. Make executable by changing permissions to 755 (using chmod on unix)

    No votes yet.
    Please wait...
  3. Hi,

    Using this script and works great. However – the output of $message shows me everything including the headers.

    Is there a way that i can get it to extract the headers and also only show the text of the email. I.e. if it comes in a html format, i want it to strip all of that away?

    Thanks!

    Matt

    No votes yet.
    Please wait...
  4. Thanks for the script, very helpful. If you have access to PEAR on your server I would recommend Pear’s http://pear.php.net/package/Mail_mimeDecode/redirected MIME Decode which is very helpful for parsing the different parts of the email into an object.

    Also, if you want to trust the POST of your email to a third party, there are some pretty good free/cheap services for this. Some of them are:
    Free services for Key Value POST to remote script:
    http://www.mailhooks.com
    http://smtp2web.com

    Paid service for Key Value, JSON and raw email posting:
    http://mailnuggets.com

    No votes yet.
    Please wait...

Leave a Comment

Your email address will not be published. Required fields are marked *