Computing.Net > Forums > Web Development > Parse Email Headers using PHP

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Parse Email Headers using PHP

Reply to Message Icon

Name: chitvan
Date: May 16, 2007 at 05:13:19 Pacific
OS: Windows/Linux
CPU/Ram: 256
Comment:

Hi All,

I am using PHP classes to parse the Email content. In the following example I have a pop3 class.

following is the code I have used:


$email = $pop3->get_mail1($i);

$email = parse_email($email);

function get_mail1( $msg_number, $qmailer = FALSE )
{
if(!$this->socket)
{
$this->error = "POP3 get_mail() - Error: No connection avalible.";

return FALSE;
}

if(!$this->_checkstate("get_mail")) return FALSE;

$response = "";
$cmd = "RETR $msg_number";
if(!$this->_logging($cmd)) return FALSE;
if(!$this->_putline($cmd)) return FALSE;

$response = $this->_getnextstring();

if(!$this->_logging($response)) return FALSE;

if ($qmailer == TRUE)
{
if(substr($response,0,1) != '.')
{
$this->error = "POP3 get_mail() - Error: ".$response;
return FALSE;
}
}
else
{
if(substr($response,0,3) != "+OK")
{
$this->error = "POP3 get_mail() - Error: ".$response;
return FALSE;
}
}

// Get MAIL !!!
$i = "0";
$response = "<HEADER> \r\n";
while(!eregi("^\.\r\n",$response))
{
if(substr($response,0,4) == "\r\n") break;
$output[$i] = $response;
$animess .=$response;
$i++;
$response = $this->_getnextstring();
}
$output[$i++] = "</HEADER> \r\n";

$response = "<MESSAGE> \r\n";

while(!eregi("^\.\r\n",$response))
{
$output[$i] = $response;
$animess .=$response;
$i++;
$response = $this->_getnextstring();
}

$output[$i] = "</MESSAGE> \r\n";

if(!$this->_logging("Complete.")) return FALSE;

return $this->getmessage($animess);
//return $output;
}

function _getnextstring( $buffer_size = 512 )
{
$buffer = "";
$buffer = fgets( $this->socket , $buffer_size );

$this->socket_status = socket_get_status( $this->socket );

if( $this->socket_status["timed_out"] )
{
$this->_cleanup();
return "POP3 _getnextstring() - Socket_Timeout_reached.";
}
$this->socket_status = FALSE;

return $buffer;
}


function parse_email ($email) {
// Split header and message
$header = array();
$message = array();

$is_header = true;
foreach ($email as $line) {
if ($line == '<HEADER> ' . "\r\n") continue;
if ($line == '<MESSAGE> ' . "\r\n") continue;
if ($line == '</MESSAGE> ' . "\r\n") continue;
if ($line == '</HEADER> ' . "\r\n") { $is_header = false; continue; }

if ($is_header == true) {
$header[] = $line;
} else {
$message[] = $line;
}
}

// Parse headers
$headers = array();

foreach ($header as $line) {
$colon_pos = strpos($line, ':');
$space_pos = strpos($line, ' ');

if ($colon_pos === false OR $space_pos < $colon_pos) {
// attach to previous
$previous .= "\r\n" . $line;
continue;
}

// Get key
$key = substr($line, 0, $colon_pos);

// Get value
$value = substr($line, $colon_pos+2);
$headers[$key] = $value;

$previous =& $headers[$key];
}
// Parse message
$message = implode('', $message);

// Return array
$email = array();
$email['message'] = $message;
$email['headers'] = $headers;
/* echo "<pre>";
echo print_r($message);;
echo "<pre> message$id";
*/ return $email;
}


In the above code I am able to fetch all the hedares and the message part. I need to fetch the mail id

from where the mail is received. Now there are 2 cases for the FROM feild:

1. abc@example.com
2. ABC XYZ <abc.xyz@example.com>

In the second case when I retrieve the header part I get "ABC XYZ" and not the other part, i.e.,

<abc.xyz@example.com>. Also I am not able to fetch the [return-path] value.

Please can someone explain where I am wrong in the above code.

thanks in advance.



Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: Parse Email Headers using PHP

php form www.computing.net/answers/webdevel/php-form/762.html

sending multiple mails www.computing.net/answers/webdevel/sending-multiple-mails/2170.html

sending multiple mails www.computing.net/answers/webdevel/sending-multiple-mails/2162.html