monderazarnino / php-mime-mail-parser

Automatically exported from code.google.com/p/php-mime-mail-parser
0 stars 0 forks source link

unable to retrieve text body in presence of text attachment #18

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. try to get the text body of email with a text file attached
2.
3.

What is the expected output? 
the text body of email

What do you see instead?
the content of attached file

What version of the product are you using? On what operating system?
trunk, on CENTOS 5.4 with php 5.2.11

Please provide any additional information below.

this occurs because the foreach cycle in getMessageBody() process every text 
part, and if more than one is present the last is returned.
Usually attachments come after the body, so for me a simple break; instruction 
at the end of cycle worked fine.

Original issue reported on code.google.com by mfris...@tin.it on 13 Nov 2010 at 4:45

GoogleCodeExporter commented 8 years ago
Thanks for mentioning. 

Original comment by buca...@gmail.com on 14 Nov 2010 at 8:32

GoogleCodeExporter commented 8 years ago

I had this problem too. 

For me works something like this:

        public function getMessageBody($type = 'text', $attach = "YES") {
                $body = false;
                $mime_types = array(
                        'text'=> 'text/plain',
                        'html'=> 'text/html'
                );
                if (in_array($type, array_keys($mime_types))) {
                        foreach($this->parts as $part) {
                                if ($this->getPartContentType($part) == $mime_types[$type]) {
                    $headers = $this->getPartHeaders($part);
                                   switch($attach) {
                                     case "NO":
                                        if((!substr(trim($headers['content-disposition']),0,10) == "attachment")) 
                                        $body .= $this->decode($this->getPartBody($part), array_key_exists('content-transfer-encoding', $headers) ? $headers['content-transfer-encoding'] : '');
                                        break;
                                     case "ONLY":
                                        if((substr(trim($headers['content-disposition']),0,10) == "attachment")) 
                                        $body .= $this->decode($this->getPartBody($part), array_key_exists('content-transfer-encoding', $headers) ? $headers['content-transfer-encoding'] : '');
                                        break;
                                     default: // YES
                                        $body .= $this->decode($this->getPartBody($part), array_key_exists('content-transfer-encoding', $headers) ? $headers['content-transfer-encoding'] : '');
                                        break;
                                   };// switch($atatch)
                                }
                        }
                } else {
                        throw new Exception('Invalid type specified for MimeMailParser::getMessageBody. "type" can either be text or html.');
                }
                return $body;
        }

Original comment by martin.j...@gmail.com on 14 Dec 2010 at 3:16

GoogleCodeExporter commented 8 years ago

public function getMessageBody($type = 'text', $attach = "YES") {

call function with $attach =
NO => text/plain attachments are not added to body
YES => text/plain attachments are added to body
ONLY => only text/plain attachments are added to body

I added dot ( $body .= ) as well, because only last text/plain was retrieved.

Original comment by martin.j...@gmail.com on 14 Dec 2010 at 3:21

GoogleCodeExporter commented 8 years ago
Thank you very much for this fix! (It also works for the same kind of problem 
with html attachments.)

Original comment by peter.pe...@gmail.com on 29 Oct 2012 at 9:45