Skip to content

Commit

Permalink
Merge pull request php-mime-mail-parser#161 from howardcox/master
Browse files Browse the repository at this point in the history
Fixes php-mime-mail-parser#158 : to not include parts from attachments in getMessageBody()
  • Loading branch information
eXorus committed Sep 30, 2017
2 parents 1fdd734 + 05444ac commit a3d5b16
Show file tree
Hide file tree
Showing 6 changed files with 380 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,37 @@ protected function getPartHeaderFromText(&$part)
return $header;
}

/**
* Checks whether a given part ID is a child of another part
* eg. an RFC822 attachment may have one or more text parts
*
* @param string $partId
* @param string $parentPartId
* @return bool
*/
protected function partIdIsChildOfPart($partId, $parentPartId)
{
return substr($partId, 0, strlen($parentPartId)) == $parentPartId;
}

/**
* Whether the given part ID is a child of any attachment part in the message.
*
* @param string $checkPartId
* @return bool
*/
protected function partIdIsChildOfAnAttachment($checkPartId)
{
foreach ($this->parts as $partId => $part) {
if ($this->getPart('content-disposition', $part) == 'attachment') {
if ($this->partIdIsChildOfPart($checkPartId, $partId)) {
return true;
}
}
}
return false;
}

/**
* Returns the email message body in the specified format
*
Expand All @@ -326,10 +357,12 @@ public function getMessageBody($type = 'text')
'html' => 'text/html',
'htmlEmbedded' => 'text/html',
];

if (in_array($type, array_keys($mime_types))) {
foreach ($this->parts as $part) {
foreach ($this->parts as $partId => $part) {
if ($this->getPart('content-type', $part) == $mime_types[$type]
&& $this->getPart('content-disposition', $part) != 'attachment'
&& !$this->partIdIsChildOfAnAttachment($partId)
) {
$headers = $this->getPart('headers', $part);
$encodingType = array_key_exists('content-transfer-encoding', $headers) ?
Expand Down
65 changes: 65 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1578,4 +1578,69 @@ public function testAttachmentGetMimePartStrFromText($mid, $attachmentMimeParts)
$i++;
}
}

/**
* @dataProvider providerRFC822AttachmentsWithDifferentTextTypes
*
* @param string $file Mail file path to parse
* @param string $getType The type to give to getMessageBody
* @param string $expected
*/
public function testRFC822AttachmentPartsShouldNotBeIncludedInGetMessageBody($file, $getType, $expected)
{
$Parser = new Parser();
$Parser->setPath($file);
$this->assertEquals($expected, $Parser->getMessageBody($getType));
}

public function providerRFC822AttachmentsWithDifferentTextTypes()
{
return [
'HTML-only message, with text-only RFC822 attachment, message should have empty text body' => [
__DIR__.'/mails/issue158a',
'text',
''
],
'HTML-only message, with text-only RFC822 attachment, message should have HTML body' => [
__DIR__.'/mails/issue158a',
'html',
"<html><body>An RFC 822 forward with a <em>HTML</em> body</body></html>\n"
],
'Text-only message, with HTML-only RFC822 attachment, message should have empty HTML body' => [
__DIR__.'/mails/issue158b',
'html',
''
],
'Text-only message, with HTML-only RFC822 attachment, message should have text body' => [
__DIR__.'/mails/issue158b',
'text',
"A text/plain response to an REC822 message, with content filler to get it past the
200 character lower-limit in order to avoid preferring future HTML versions of the
body... filler filler filler filler filler filler filler filler filler.\n"
],
'Text-only message, with text-only RFC822 attachment,
should have text body but not include attachment part' => [
__DIR__.'/mails/issue158c',
'text',
"An RFC822 forward of a PLAIN TEXT message with a plain-text body.\n"
],
'Text-only message, with a text-only RFC822 attachment, message should have an empty HTML body' => [
__DIR__.'/mails/issue158c',
'html',
''
],
'Multipart email with both text and html body, with RFC822 attachment also with a text and html body' => [
__DIR__.'/mails/issue158d',
'text',
"This is the forward email send both emails will have both text and html variances available\n"
],
'Multipart with both text and html body, RFC822 attachment also with text and html' => [
__DIR__.'/mails/issue158d',
'html',
'<html><body><div>This is the forward email send both
emails will have both text and html
variances available &nbsp;</div></body></html>'
],
];
}
}
68 changes: 68 additions & 0 deletions tests/mails/issue158a
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
Date: Mon, 6 Mar 2017 14:56:51 +0000 (GMT)
From: Example Name <example@example.com>
To: example@example.co.uk
Message-ID: <445158045.378131.1488812211066.JavaMail.zimbra@zedcore.com>
In-Reply-To: <649983063.377570.1488812041831.JavaMail.zimbra@zedcore.com>
References: <649983063.377570.1488812041831.JavaMail.zimbra@zedcore.com>
Subject: Fwd: Test 5
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_378129_257370687.1488812211065"
X-Mailer: Zimbra 8.6.0_GA_1200 (ZimbraWebClient - FF45 (Linux)/8.6.0_GA_1200)
Thread-Topic: Test 5
Thread-Index: BsIxD6EDQhVNqaTfcPN/d36x7UYGq6dRLyIX

------=_Part_378129_257370687.1488812211065
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit

<html><body>An RFC 822 forward with a <em>HTML</em> body</body></html>

------=_Part_378129_257370687.1488812211065
Content-Type: message/rfc822
Content-Disposition: attachment

Return-Path: example@example.com
Received: from fermat.axiomtech.co.uk (LHLO fermat.axiomtech.co.uk)
(89.145.71.216) by fermat.axiomtech.co.uk with LMTP; Mon, 6 Mar 2017
14:54:02 +0000 (GMT)
Received: from localhost (localhost.localdomain [127.0.0.1])
by fermat.axiomtech.co.uk (Postfix) with ESMTP id 174DF1C407FD
for <example@example.com>; Mon, 6 Mar 2017 14:54:02 +0000 (GMT)
X-Spam-Flag: NO
X-Spam-Score: -2.9
X-Spam-Level:
X-Spam-Status: No, score=-2.9 tagged_above=-10 required=6.6
tests=[ALL_TRUSTED=-1, BAYES_00=-1.9] autolearn=ham autolearn_force=no
Received: from fermat.axiomtech.co.uk ([127.0.0.1])
by localhost (fermat.axiomtech.co.uk [127.0.0.1]) (amavisd-new, port 10032)
with ESMTP id G83UfQTDA5rx for <example@example.com>;
Mon, 6 Mar 2017 14:54:01 +0000 (GMT)
Received: from localhost (localhost.localdomain [127.0.0.1])
by fermat.axiomtech.co.uk (Postfix) with ESMTP id DEF301C40805
for <example@example.com>; Mon, 6 Mar 2017 14:54:01 +0000 (GMT)
X-Virus-Scanned: amavisd-new at fermat.axiomtech.co.uk
Received: from fermat.axiomtech.co.uk ([127.0.0.1])
by localhost (fermat.axiomtech.co.uk [127.0.0.1]) (amavisd-new, port 10026)
with ESMTP id g5r-rGDF7iTG for <example@example.com>;
Mon, 6 Mar 2017 14:54:01 +0000 (GMT)
Received: from fermat.axiomtech.co.uk (fermat.axiomtech.co.uk [89.145.71.216])
by fermat.axiomtech.co.uk (Postfix) with ESMTP id CD9B21C407FD
for <example@example.co.uk>; Mon, 6 Mar 2017 14:54:01 +0000 (GMT)
Date: Mon, 6 Mar 2017 14:54:01 +0000 (GMT)
From: Example Name <example@example.com>
To: example@example.co.uk
Message-ID: <649983063.377570.1488812041831.JavaMail.zimbra@example.com>
Subject: Test 5
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
X-Mailer: Zimbra 8.6.0_GA_1200 (ZimbraWebClient - FF45 (Linux)/8.6.0_GA_1200)
Thread-Topic: Test 5
Thread-Index: BsIxD6EDQhVNqaTfcPN/d36x7UYGqw==

An original PLAIN TEXT message body, with content filler to get it past the
200 character lower-limit in order to avoid preferring future HTML versions of the
body... filler filler filler filler filler filler filler filler filler.

------=_Part_378129_257370687.1488812211065--
67 changes: 67 additions & 0 deletions tests/mails/issue158b
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
Date: Mon, 6 Mar 2017 14:56:51 +0000 (GMT)
From: Example Name <example@example.com>
To: example@example.co.uk
Message-ID: <445158045.378131.1488812211066.JavaMail.zimbra@zedcore.com>
In-Reply-To: <649983063.377570.1488812041831.JavaMail.zimbra@zedcore.com>
References: <649983063.377570.1488812041831.JavaMail.zimbra@zedcore.com>
Subject: Fwd: Test 5
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_378129_257370687.1488812211065"
X-Mailer: Zimbra 8.6.0_GA_1200 (ZimbraWebClient - FF45 (Linux)/8.6.0_GA_1200)
Thread-Topic: Test 5
Thread-Index: BsIxD6EDQhVNqaTfcPN/d36x7UYGq6dRLyIX

------=_Part_378129_257370687.1488812211065
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit

A text/plain response to an REC822 message, with content filler to get it past the
200 character lower-limit in order to avoid preferring future HTML versions of the
body... filler filler filler filler filler filler filler filler filler.

------=_Part_378129_257370687.1488812211065
Content-Type: message/rfc822
Content-Disposition: attachment

Return-Path: example@example.com
Received: from fermat.axiomtech.co.uk (LHLO fermat.axiomtech.co.uk)
(89.145.71.216) by fermat.axiomtech.co.uk with LMTP; Mon, 6 Mar 2017
14:54:02 +0000 (GMT)
Received: from localhost (localhost.localdomain [127.0.0.1])
by fermat.axiomtech.co.uk (Postfix) with ESMTP id 174DF1C407FD
for <example@example.com>; Mon, 6 Mar 2017 14:54:02 +0000 (GMT)
X-Spam-Flag: NO
X-Spam-Score: -2.9
X-Spam-Level:
X-Spam-Status: No, score=-2.9 tagged_above=-10 required=6.6
tests=[ALL_TRUSTED=-1, BAYES_00=-1.9] autolearn=ham autolearn_force=no
Received: from fermat.axiomtech.co.uk ([127.0.0.1])
by localhost (fermat.axiomtech.co.uk [127.0.0.1]) (amavisd-new, port 10032)
with ESMTP id G83UfQTDA5rx for <example@example.com>;
Mon, 6 Mar 2017 14:54:01 +0000 (GMT)
Received: from localhost (localhost.localdomain [127.0.0.1])
by fermat.axiomtech.co.uk (Postfix) with ESMTP id DEF301C40805
for <example@example.com>; Mon, 6 Mar 2017 14:54:01 +0000 (GMT)
X-Virus-Scanned: amavisd-new at fermat.axiomtech.co.uk
Received: from fermat.axiomtech.co.uk ([127.0.0.1])
by localhost (fermat.axiomtech.co.uk [127.0.0.1]) (amavisd-new, port 10026)
with ESMTP id g5r-rGDF7iTG for <example@example.com>;
Mon, 6 Mar 2017 14:54:01 +0000 (GMT)
Received: from fermat.axiomtech.co.uk (fermat.axiomtech.co.uk [89.145.71.216])
by fermat.axiomtech.co.uk (Postfix) with ESMTP id CD9B21C407FD
for <example@example.co.uk>; Mon, 6 Mar 2017 14:54:01 +0000 (GMT)
Date: Mon, 6 Mar 2017 14:54:01 +0000 (GMT)
From: Example Name <example@example.com>
To: example@example.co.uk
Message-ID: <649983063.377570.1488812041831.JavaMail.zimbra@example.com>
Subject: Test 5
MIME-Version: 1.0
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit
X-Mailer: Zimbra 8.6.0_GA_1200 (ZimbraWebClient - FF45 (Linux)/8.6.0_GA_1200)
Thread-Topic: Test 5
Thread-Index: BsIxD6EDQhVNqaTfcPN/d36x7UYGqw==

<html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div>A text/<em>HTML</em> body this is the <strong>original</strong> message <span data-mce-style="text-decoration: underline;" style="text-decoration: underline;">sent</span>.<br></div><div><br data-mce-bogus="1"></div><div>Filler filler filler filler filler filler filler filler</div><div> filler filler filler filler filler filler filler</div><div> filler filler filler filler filler filler<br></div><div> filler filler filler filler filler</div><div> filler filler filler filler</div><div> filler filler filler</div><div> filler filler</div><div> filler.</div></div></body></html>
------=_Part_378129_257370687.1488812211065--
68 changes: 68 additions & 0 deletions tests/mails/issue158c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
Date: Mon, 6 Mar 2017 14:56:51 +0000 (GMT)
From: Example Name <example@example.com>
To: example@example.co.uk
Message-ID: <445158045.378131.1488812211066.JavaMail.zimbra@zedcore.com>
In-Reply-To: <649983063.377570.1488812041831.JavaMail.zimbra@zedcore.com>
References: <649983063.377570.1488812041831.JavaMail.zimbra@zedcore.com>
Subject: Fwd: Test 5
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_378129_257370687.1488812211065"
X-Mailer: Zimbra 8.6.0_GA_1200 (ZimbraWebClient - FF45 (Linux)/8.6.0_GA_1200)
Thread-Topic: Test 5
Thread-Index: BsIxD6EDQhVNqaTfcPN/d36x7UYGq6dRLyIX

------=_Part_378129_257370687.1488812211065
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit

An RFC822 forward of a PLAIN TEXT message with a plain-text body.

------=_Part_378129_257370687.1488812211065
Content-Type: message/rfc822
Content-Disposition: attachment

Return-Path: example@example.com
Received: from fermat.axiomtech.co.uk (LHLO fermat.axiomtech.co.uk)
(89.145.71.216) by fermat.axiomtech.co.uk with LMTP; Mon, 6 Mar 2017
14:54:02 +0000 (GMT)
Received: from localhost (localhost.localdomain [127.0.0.1])
by fermat.axiomtech.co.uk (Postfix) with ESMTP id 174DF1C407FD
for <example@example.com>; Mon, 6 Mar 2017 14:54:02 +0000 (GMT)
X-Spam-Flag: NO
X-Spam-Score: -2.9
X-Spam-Level:
X-Spam-Status: No, score=-2.9 tagged_above=-10 required=6.6
tests=[ALL_TRUSTED=-1, BAYES_00=-1.9] autolearn=ham autolearn_force=no
Received: from fermat.axiomtech.co.uk ([127.0.0.1])
by localhost (fermat.axiomtech.co.uk [127.0.0.1]) (amavisd-new, port 10032)
with ESMTP id G83UfQTDA5rx for <example@example.com>;
Mon, 6 Mar 2017 14:54:01 +0000 (GMT)
Received: from localhost (localhost.localdomain [127.0.0.1])
by fermat.axiomtech.co.uk (Postfix) with ESMTP id DEF301C40805
for <example@example.com>; Mon, 6 Mar 2017 14:54:01 +0000 (GMT)
X-Virus-Scanned: amavisd-new at fermat.axiomtech.co.uk
Received: from fermat.axiomtech.co.uk ([127.0.0.1])
by localhost (fermat.axiomtech.co.uk [127.0.0.1]) (amavisd-new, port 10026)
with ESMTP id g5r-rGDF7iTG for <example@example.com>;
Mon, 6 Mar 2017 14:54:01 +0000 (GMT)
Received: from fermat.axiomtech.co.uk (fermat.axiomtech.co.uk [89.145.71.216])
by fermat.axiomtech.co.uk (Postfix) with ESMTP id CD9B21C407FD
for <example@example.co.uk>; Mon, 6 Mar 2017 14:54:01 +0000 (GMT)
Date: Mon, 6 Mar 2017 14:54:01 +0000 (GMT)
From: Example Name <example@example.com>
To: example@example.co.uk
Message-ID: <649983063.377570.1488812041831.JavaMail.zimbra@example.com>
Subject: Test 5
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
X-Mailer: Zimbra 8.6.0_GA_1200 (ZimbraWebClient - FF45 (Linux)/8.6.0_GA_1200)
Thread-Topic: Test 5
Thread-Index: BsIxD6EDQhVNqaTfcPN/d36x7UYGqw==

An original PLAIN TEXT message body, with content filler to get it past the
200 character lower-limit in order to avoid preferring future HTML versions of the
body... filler filler filler filler filler filler filler filler filler.

------=_Part_378129_257370687.1488812211065--
Loading

0 comments on commit a3d5b16

Please sign in to comment.