Hi guys
I've been experimenting with email piping and have arrived at a script, with tutorial and a little forum searching with this code, that more or less eliminates the headers from the emails I'm throwing at it with google.
At first the code resulted in emails like this
--0016363b85b0699a9b046c550c1d
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
In a time long long ago ... bla bla blah
--0016363b85b0699a9b046c550c1d
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
In a time long long ago ... bla bla blah<br>
--0016363b85b0699a9b046c550c1d--
After adding some code I found it moved me along the right track and produced emails like this
--0016e647ee5ee5bea5046c565fdc
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
of the piping system please work!
Here is the code I am using:
#!/usr/local/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;
// organise email
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;
}
}
preg_match("/boundary=\".*?\"/i", $headers, $boundary);
$boundaryfulltext = $boundary[0];
if ($boundaryfulltext!="")
{
$find = array("/boundary=\"/i", "/\"/i");
$boundarytext = preg_replace($find, "", $boundaryfulltext);
$splitmessage = explode("--" . $boundarytext, $message);
$fullmessage = ltrim($splitmessage[1]);
preg_match('/\n\n(.*)/is', $fullmessage, $splitmore);
if (substr(ltrim($splitmore[0]), 0, 2)=="--")
{
$actualmessage = $splitmore[0];
}
else
{
$actualmessage = ltrim($splitmore[0]);
}
}
else
{
$actualmessage = ltrim($message);
}
$clean = array("/\n--.*/is", "/=3D\n.*/s");
$cleanmessage = trim(preg_replace($clean, "", $actualmessage));
// test action to be replaced by db insertion code
$body = "$cleanmessage";
mail('test@test.com', '$subject', $body, 'From: pipe@test.com');
?>
I'm hoping that someone can help me fine tune this so I can try and at least mostly solve the header handling to a point where I have a longer list of compatible email systems that will work correctly with the pipe script.
I am attempting to create a play by email system that can be used for roleplay games and other forms of mailing lists.
Thanks!