Jump to content

parsing emails


digitalgod

Recommended Posts

hey guys,

 

I'm trying to parse emails and store them in a db, everything works fine except for the fact that no matter what the body of the email is, I always get this message in the db

 

This is a multipart message in MIME format.<br />

 

any ideas why it does that? I'm using pop3 and here's my parse function

 

$email = $pop3->get_mail($i);
$email = parse_email ($email);

echo nl2br(htmlentities($email['message']));

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; 

    return $email; 
} 

 

*edit*

I forgot to ask, is there any way of flagging an email as read without actually deleting it from the server?

Link to comment
https://forums.phpfreaks.com/topic/92193-parsing-emails/
Share on other sites

I only started learning about parsing emails the other day, so I'm not an expert, but I think I know your problem.

 

When an email is sent out as a multipart document, one part is the header, one part the message, one part an attachment, etc.  If the email is sent out as HTML, however, I believe this is another part entirely. So you may want to try to parse for the HTML part.

 

Just an idea. Wish I could help more.

Link to comment
https://forums.phpfreaks.com/topic/92193-parsing-emails/#findComment-472424
Share on other sites

  • 1 month later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.