getMessageBody may have unexpected behavior for emails with multiple parts with same requested mime type
GoogleCodeExporter opened this issue · comments
If there is more than one part the a mime type of the requested type then
the code loops through and returns the last one in the list. Is this
expected behavior? At least with Apple Mail, when there is more than one
part of mime type 'text/plain', it seems that the first one is the one you
would want, not the last.
I have attached a sample message that exhibits the behavior.
The following code will reproduce:
<code>
$parser = new MimeMailParser();
$parser->setPath('mail.txt');
$msg = $parser->getMessageBody();
echo "Message found: " . $message; //should be 'Test message'
</code>
Original issue reported on code.google.com by ks2...@gmail.com
on 16 Apr 2010 at 11:04
Attachments:
Not that you still need this code, but the issue is a missing '.' in the main
class.
replace:
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;
}
with:
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;
}
Original comment by Jud.Step...@gmail.com
on 14 Dec 2010 at 6:23
Sorry, but notice that the variable "$body =" and "$body .="
Original comment by Jud.Step...@gmail.com
on 14 Dec 2010 at 6:24
"$body .=" don't solve the problem. This only join all text (or html) parts of
email.
you must check in foreach, if $part is attachment or not.
same problem is in function getMessageBodyHeaders
Original comment by tomas.kr...@gmail.com
on 30 Mar 2012 at 1:17
Not only does getMessageBody() potentially return the wrong part it always
returns the LAST matching part instead of the first (its possible to have a
text/plain message and a text/plain attachment).
You should return at the first match. If this were on github I would submit a
pull request with my fix, but instead see my updated method attached. I ignore
attachments and return on the first match found.
Original comment by lifo...@gmail.com
on 28 Aug 2012 at 3:30
Attachments:
This issue is fixed from now on.
Original comment by M.Valins...@gmail.com
on 24 Nov 2012 at 10:49
- Changed state: Fixed