Parse Email Headers

Reply

Join Date: Sep 2005
Posts: 1,085
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Parse Email Headers

 
0
  #1
May 19th, 2006
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.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 8947 | Replies: 0
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC