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
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
Share on other sites

  • 1 month later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.