Technology ramblings of a pro breaker and fixer.
Categories

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

Pipe email to a php script, 5.0 out of 5 based on 2 ratings

4 Responses to “Pipe email to a php script”

  • Globals - October 3, 2009 at 7:14 pm

    all good things

    VA:F [1.9.22_1171]
    Rating: +1 (from 1 vote)
  • TheAnswer - August 17, 2010 at 10:59 pm

    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)

    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  • Matt - September 7, 2010 at 11:20 am

    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

    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  • Marty - October 3, 2010 at 4:15 pm

    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

    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)

Leave a Reply for Globals