Hi there guys,
I've made some progress since I last posted, but have run into an issue that I'm so far unable to solve with the assistance of google.
I've tried to take apart the message, so I can store from name, from email, to name, to email, etc. but it's not saving the proper items. Here's the important part:
$fd = fopen("php://stdin", "r");
$email_content = "";
while (!feof($fd)) {
$email_content .= fread($fd, 1024);
}
fclose($fd);
//split the string into array of strings, each of the string represents a single line, received
$lines = explode("\n", $email_content);
// initialize variable which will assigned later on
$from = "";
$subject = "";
$headers = "";
$message = "";
$is_header= true;
//loop through each line
for ($i=0; $i < count($lines); $i++) {
if ($is_header) {
// hear information. instead of main message body, all other information are here.
$headers .= $lines[$i]."\n";
// Split out the To portion
if (preg_match("/^To: (.*)/", $lines[$i], $matches)) {
$to = $matches[1];
}
$toregexp = '/To:\s*(([^\<]*?) <)?<?(.+?)>?\s*\n/i';
if(preg_match($toregexp, $email_content, $to_dissection)) {
$toname = $to_dissection[2];
$toemail = $to_dissection[3];
}
// Split out the subject portion
if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
$subject = $matches[1];
}
//Split out the sender information portion
if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
$from = $matches[1];
}
$fromregexp = '/From:\s*(([^\<]*?) <)?<?(.+?)>?\s*\n/i';
if(preg_match($fromregexp, $email_content, $from_dissection)) {
$fromname = $from_dissection[2];
$fromemail = $from_dissection[3];
}
} else {
// content/main message body information
$message .= $lines[$i]."\n";
}
if (trim($lines[$i])=="") {
// empty line, header section has ended
$is_header = false;
}
}
I've got it storing the message into the database, but it's storing improper data for the addresses. I've clearly messed up in the regex department. Is there a better way to handle the grabbing of the various parts of an email than what I'm trying to do?
Thanks for your time!