Incoming Mail and PHP

Submitted by rmiddle on Sat, 04/04/2009 - 19:20

We all know how to send email from PHP. Actually, it's quite easy: mail("to@me", "Hello", "Hello"); Handling mail the other way, sending email to PHP is a task much more unknown. In this article, we will write and install a script that we can send an email to. 

http://www.evolt.org/article/Incoming_Mail_and_PHP/18/27914/index.html

<code>
#!/usr/bin/php
< ?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;
    }
}
</code>

For my needs I needed to take an email generated from an automated system and change a few fields and resubmit the email.  So I email address email.phpfilter@example.com and using postfix I accept the email and send it to the following php function.

</code><code>
#!/usr/bin/php
< ?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);

// $to is the final destination of the email
$to      = "email@example.com";
// On of the files I need to change is the reply-to because the generated system reply address doesn't
// work for my needs.
$replyto = "Reply-To: email.gateway@example.com";

// empty vars
$subject = "Blank";
$headers = "";
$message = "";
$splittingheaders = true;

for ($i=0; $i < count($lines); $i++) {
    if ($splittingheaders) {
        // look out for special headers
      if (!preg_match("/^To: (.*)/", $lines[$i], $matches)) { // If To just drop it we don't need that field.
        if (!preg_match("/^Subject: (.*)/", $lines[$i], $matches)) { // If subject again just drop it.  
          // Anything other then To or Subject store the header.
          $headers .= $lines[$i]."\n";
        } else {
          // We are ignoring the Subject but this is a good place to insert my Reply-To address.
          $headers .= $replyto."\n";
        }
      }
    } else {
        // not a header, but message
        // If a line inside the message == Subject: then grab that out and assign to the Var Subject.  
        // Else just store the message subject.
        if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
          $subject = $matches[1];
        } else {
          $message .= $lines[$i]."\n";
        }
    }

    if (trim($lines[$i])=="") {
        // empty line, header section has ended
        $splittingheaders = false;
    }
}
// Last step email the new redefined messages.
mail($to, $subject, $message, $headers);
</code>

It works pretty good with the one except.  It inserts a blank line at the top of the message.  At some point I might track that down but it isn't an issue for my use.</code>

tax_term_view
Category Tags
Programming email, php