| | |
Parse Email Headers
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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.
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
- Need Web Based Contact Us Form (PHP)
- Hebrew & The mail() Function (PHP)
- send email with attachment (PHP)
- Parsing html form. (PHP)
- Sending HTML/Plain text Emails (PHP)
- HELP: created dreamweaver form, but how do i get results from form? (PHP)
- Simple Email Form (PHP)
- Net analysis tool to try (Viruses, Spyware and other Nasties)
Other Threads in the PHP Forum
- Previous Thread: PHP cookie question
- Next Thread: script problem adding to database
Views: 8947 | Replies: 0
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date directory display download dynamic ebooks echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla jquery js limit link login loop mail mediawiki menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap source space speed sql stored structure subdomain syntax system table tutorial update updates upload url validation validator variable video web xml youtube






