Jump to content

Need help dissecting and storing email components (headers, addresses, body, etc.)


schwim

Recommended Posts

Hi there everyone!

I'm trying my best to Google my way through this issue, but have run into an issue I can't out-Google. I'm trying to write a script that is receiving an email, breaking it apart into it's various components and then storing into a database. I would like to store:

from name
from email
to name
to email
subject
headers
body

Here's what I've cobbled together from various tutorials and demo's:

$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;
    }
}

My issues are many. I've got from and to names that don't reflect the true data,
the from address will be the original sender and not the most recent sender if
it's forwarded and finally, the body is showing up empty. Clearly, I'm not handling this properly. With all the trouble-ticket like systems out there, there has to be a fairly bulletproof method of handling this, doesn't there?  I'm thinking I've gone about this totally the wrong way.

Any help would be greatly appreciated!

 
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.