944,199 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 11621
  • PHP RSS
May 19th, 2006
0

Parse Email Headers

Expand Post »
Does anyone know of a class that is available open source that is used for parsing Raw Emails.

I've seen the code where you first explode the raw email into lines, then iterate through the lines until you come to an empty line that seperates the headers from the body of the email. However, this does not work on all emails.

Eg:

[php]
$raw_email = get_raw_email_imap_or_pop_etc();

parseEmail($raw_email) {

/**
* Would be nice to get a list of headers
* And the Body
* Returned as an object.
*/

}

[/php]

Heres the code I have right now, that does not seem to work on all the emails I've tested (especially those from gmail).

[php]
/**
* Parse the email and return parts
*/
function getParts($content) {
$in_head = true;
//$lines = preg_split("/\r|\n||\r\n/", $content);
$lines = explode("\n", $content);
$n = $this->linecount = count($lines);
$this->headers = array();
$this->message = '';

for ($i=0; $i < $n; $i++) {
$line = trim($lines[$i]);
if ($in_head) {
// header
$matches = explode(':', $line);
$key = array_shift($matches);
$this->headers[$key] = implode(':', $matches);

} else {
// message
$this->message .= $line."\r\n";
}
if ($line == '') {
// empty line, headers have ended
$in_head = false;
}
}
}
[/php]

Its part of a larger class, but all thats needed is there. $content is the raw email with all email headers and body.
Similar Threads
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP cookie question
Next Thread in PHP Forum Timeline: script problem adding to database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC