Jump to content

mailparse


yheffen

Recommended Posts

I want to extract the body and attachment from an email. I would expect the mailparse package to do just this, but I am having problems. The mailparse documentation, http://us3.php.net/manual/en/book.mailparse.php, isn't giving me the clues that I need. I can extract the headers of the messages, but I want to extract the bodies and attachments of each MIME part, and the parts happen to pretty much all be Base64 encoded, even the body.

 

I would expect this to be a pretty common thing, but I can't find mailparse examples to do this. The big problems I'm having with the documentation is that is not clear how to tell the functions to operate on parts of a MIME message rather than the message as a whole, or how to figure out what parts a message has in the first place.

 

Anyone know of examples or can offer help? Or have recommendations for a different extension or class to use?

Link to comment
Share on other sites

Yep, I've tried to figure out how to use those to get started, but I seem to be missing the last step. The mailparse_msg_get_structure() function will correctly show the structure of my email. I can then use mailparse_msg_get_part() to zero in on the MIME section that I want... But once I get down to there, a mailparse_mail_structure resource of the piece of the email that I want, I can't figure out which function can then pull the body out of there. I can use mailparse_msg_get_part_data() to pull the header out of the section, but how do I pull out the (decoded) body?

 

Here's example code,

 

<?php

        $msg = mailparse_msg_create();

        $msg = mailparse_msg_parse_file('cattool.eml');

        $msgstruct = mailparse_msg_get_structure($msg);
        var_dump($msgstruct);

        $msgpart = mailparse_msg_get_part($msg, "1.1");
        var_dump($msgpart);

        $msgdata = mailparse_msg_get_part_data($msgpart);
        var_dump($msgdata);

?>

 

That spits out,

 

array(3) {
  [0]=>
  string(1) "1"
  [1]=>
  string(3) "1.1"
  [2]=>
  string(3) "1.2"
}
resource( of type (mailparse_mail_structure)
array(11) {
  ["headers"]=>
  array(2) {
    ["content-type"]=>
    string(9) "text/HTML"
    ["content-transfer-encoding"]=>
    string(6) "base64"
  }
  ["starting-pos"]=>
  int(868)
  ["starting-pos-body"]=>
  int(927)
  ["ending-pos"]=>
  int(133536)
  ["ending-pos-body"]=>
  int(133536)
  ["line-count"]=>
  int(2044)
  ["body-line-count"]=>
  int(2041)
  ["charset"]=>
  string( "us-ascii"
  ["transfer-encoding"]=>
  string(6) "base64"
  ["content-type"]=>
  string(9) "text/html"
  ["content-base"]=>
  string(1) "/"
}

 

 

Link to comment
Share on other sites

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.