digital-ether 399 Nearly a Posting Virtuoso Team Colleague

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:

$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.
*/

}

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

/**
	* 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;
			}
		}
	}

Its part of a larger class, but all thats needed is there. $content is the raw email with all email headers and body.