So I have setup email piping to run a script everytime email is sent to me@mydomain.com, that works no problemo.
What I want to do is save the message body to my database but I am unable to extract only the message body... (not sure how to do this).
I noticed that be it, hotmail, outlook, gmail or whatever - they all send the email in a different format, so its very hard to preg_match somthing consistant, but i am assuming there must be a way to do this ....
this is the code that I am using... it works well for everything except the message body
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
$lines = explode("\n", $email);
$from = "";
$subject = "";
$headers = "";
$message = "";
$splittingheaders = true;
for ($i=0; $i < count($lines); $i++) {
if ($splittingheaders) {
$headers .= $lines[$i]."\n";
if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
$subject = $matches[1];
}
if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
$from = $matches[1];
}
} else {
$message .= $lines[$i]."\n";
}
if (trim($lines[$i])=="") {
$splittingheaders = false;
}
}
The $message variable from outlook looks somthing like this :
This is a multi-part message in MIME format.
------=_NextPart_000_000D_01C773C8.2566DC60
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
test
------=_NextPart_000_000D_01C773C8.2566DC60
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2900.2995" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>test</FONT></DIV></BODY></HTML>
------=_NextPart_000_000D_01C773C8.2566DC60--
From gmail looks like this :
------=_Part_219212_6929206.1175392799097
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
1234
------=_Part_219212_6929206.1175392799097
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
1234
------=_Part_219212_6929206.1175392799097--
and so forth....
thanks for any help