Skip to content

Commit 5da4a0c

Browse files
authored
base64_decode may return false in PHP < 7.1 (#324)
Closes #284
1 parent fd50d60 commit 5da4a0c

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

src/Message/AbstractPart.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,10 @@ final public function getDecodedContent(): string
336336
$content = \quoted_printable_decode($content);
337337
}
338338

339+
if (false === $content) {
340+
throw new UnexpectedEncodingException('Cannot decode content');
341+
}
342+
339343
// If this part is a text part, convert its charset to UTF-8.
340344
// We don't want to decode an attachment's charset.
341345
if (!$this instanceof Attachment && null !== $this->getCharset() && self::TYPE_TEXT === $this->getType()) {

tests/MessageTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,23 @@ public function test_imap_mime_header_decode_returns_false()
903903
$this->assertSame('=?UTF-8?B?nnDusSNdG92w6Fuw61fMjAxOF8wMy0xMzMyNTMzMTkzLnBkZg==?=', $message->getSubject());
904904
}
905905

906+
public function testBooleanDecodedContent()
907+
{
908+
if (PHP_VERSION_ID >= 70100) {
909+
$this->markTestSkipped('Requires PHP < 7.1');
910+
}
911+
912+
$this->mailbox->addMessage($this->getFixture('boolean_decoded_content'));
913+
914+
$message = $this->mailbox->getMessage(1);
915+
$attachments = $message->getAttachments();
916+
$attachment = \current($attachments);
917+
918+
$this->expectException(UnexpectedEncodingException::class);
919+
920+
$attachment->getDecodedContent();
921+
}
922+
906923
private function resetAttachmentCharset(MessageInterface $message)
907924
{
908925
// Mimic GMAIL behaviour that correctly doesn't report charset
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
3+
Subject: Nuu
4+
Date: Wed, 13 Sep 2017 13:05:45 +0200
5+
Content-Type: multipart/mixed; boundary="=-vyqYb0SSRwuGFKv/Trdf"
6+
7+
--=-vyqYb0SSRwuGFKv/Trdf
8+
Content-Type: multipart/alternative; boundary="=-ewUvwipK68Y6itClYNpy"
9+
10+
--=-ewUvwipK68Y6itClYNpy
11+
Content-Type: text/plain; charset="us-ascii"
12+
13+
Here is the problem mail
14+
15+
Body text
16+
--=-ewUvwipK68Y6itClYNpy
17+
Content-Type: text/html; charset="us-ascii"
18+
19+
Here is the problem mail
20+
21+
Body text
22+
--=-ewUvwipK68Y6itClYNpy--
23+
24+
--=-vyqYb0SSRwuGFKv/Trdf
25+
Content-Type: application/pdf; name="Example Domain.pdf"
26+
Content-Disposition: attachment; filename="Example Domain.pdf"
27+
Content-Transfer-Encoding: base64
28+
29+
nnDusSNdG92w6Fuw61fMjAxOF8wMy0xMzMyNTMzMTkzLnBkZg==?=
30+
31+
--=-vyqYb0SSRwuGFKv/Trdf--

0 commit comments

Comments
 (0)