Extracting body of the mail - when there is an attachment

586 views Asked by At

I need to extract the body of a mail in php.

Currently I am using mimemailparser "getmessagebody" function for retrieving body of the mail.

it works well when there is a mail without any other (.msg) file has an attachment, but incase if the mail contains an attachment which is also a mail in .msg format, it gets the body of the attachment and not the body of the current mail. using following function and code.

public function getMessageBody($type = 'text') {
        $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);
                    $body = $this->decode($this->getPartBody($part), array_key_exists('content-transfer-encoding', $headers) ? $headers['content-transfer-encoding'] : '');
                }
            }
        } else {
            throw new Exception('Invalid type specified for MimeMailParser::getMessageBody. "type" can either be text or html.');
        }
        return $body;
    }

code:

$this->parsed->setText($this->mail);
$this->message = $this->parsed->getMessageBody('text');

Problem: getmessagebody retrieves body of the attached mail and not the original mail. any solution pls?

1

There are 1 answers

0
user3145195 On BEST ANSWER

Found the answer to this question. getmessagebody -> Actually it always returns the LAST matching part instead of the first (its possible to have a text/plain message and a text/plain attachment).

http://code.google.com/p/php-mime-mail-parser/issues/attachmentText?id=10&aid=100004000&name=getMessageBody.txt&token=vCOlNmTnFSlEGmUs31i5tY_KkoQ%3A1388629941070