Skip to content

Commit

Permalink
Manageable UnexpectedEncodingException (#282)
Browse files Browse the repository at this point in the history
Closes #278
  • Loading branch information
Slamdunk committed Jan 15, 2018
1 parent 3412710 commit 4d98eb2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/Message/AbstractPart.php
Expand Up @@ -277,6 +277,10 @@ final public function getPartNumber(): string
final public function getDecodedContent(): string
{
if (null === $this->decodedContent) {
if (self::ENCODING_UNKNOWN === $this->getEncoding()) {
throw new UnexpectedEncodingException('Cannot decode a content with an uknown encoding');
}

$content = $this->getContent();
if (self::ENCODING_BASE64 === $this->getEncoding()) {
$content = \base64_decode($content);
Expand Down Expand Up @@ -322,11 +326,8 @@ private function parseStructure(\stdClass $structure)
{
$this->type = $this->typesMap[$structure->type] ?? self::TYPE_UNKNOWN;

if (!isset($this->encodingsMap[$structure->encoding])) {
throw new UnexpectedEncodingException(\sprintf('Cannot decode "%s"', $structure->encoding));
}

$this->encoding = $this->encodingsMap[$structure->encoding];
// In our context, \ENCOTHER is as useful as an uknown encoding
$this->encoding = $this->encodingsMap[$structure->encoding] ?? self::ENCODING_UNKNOWN;
$this->subtype = $structure->subtype;

foreach (['disposition', 'bytes', 'description'] as $optional) {
Expand Down
1 change: 1 addition & 0 deletions src/Message/PartInterface.php
Expand Up @@ -25,6 +25,7 @@ interface PartInterface extends \RecursiveIterator
const ENCODING_BINARY = 'binary';
const ENCODING_BASE64 = 'base64';
const ENCODING_QUOTED_PRINTABLE = 'quoted-printable';
const ENCODING_UNKNOWN = 'unknown';

const SUBTYPE_PLAIN = 'PLAIN';
const SUBTYPE_HTML = 'HTML';
Expand Down
24 changes: 24 additions & 0 deletions tests/MessageTest.php
Expand Up @@ -6,10 +6,12 @@

use Ddeboer\Imap\Exception\InvalidDateHeaderException;
use Ddeboer\Imap\Exception\MessageDoesNotExistException;
use Ddeboer\Imap\Exception\UnexpectedEncodingException;
use Ddeboer\Imap\Exception\UnsupportedCharsetException;
use Ddeboer\Imap\Message;
use Ddeboer\Imap\Message\EmailAddress;
use Ddeboer\Imap\Message\Parameters;
use Ddeboer\Imap\Message\PartInterface;
use Ddeboer\Imap\Message\Transcoder;
use Ddeboer\Imap\MessageIterator;
use Ddeboer\Imap\Search;
Expand Down Expand Up @@ -837,6 +839,28 @@ public function testNoMessageId()
$this->assertNull($message->getId());
}

public function testUnknownEncodingIsManageable()
{
$this->mailbox->addMessage($this->getFixture('unknown_encoding'));

$message = $this->mailbox->getMessage(1);

$parts = [];
foreach ($message->getParts() as $part) {
$parts[$part->getSubtype()] = $part;
}

$this->assertArrayHasKey(PartInterface::SUBTYPE_PLAIN, $parts);

$plain = $parts[PartInterface::SUBTYPE_PLAIN];

$this->assertSame(PartInterface::ENCODING_UNKNOWN, $plain->getEncoding());

$this->expectException(UnexpectedEncodingException::class);

$plain->getDecodedContent();
}

private function resetAttachmentCharset(Message $message)
{
// Mimic GMAIL behaviour that correctly doesn't report charset
Expand Down
24 changes: 24 additions & 0 deletions tests/fixtures/unknown_encoding.eml
@@ -0,0 +1,24 @@
From: from@there.com
To: to@here.com
Date: Wed, 27 Sep 2017 12:48:51 +0200
Subject: test
Content-Type: multipart/alternative;
boundary="----=_NextPart_000_0081_01D32C91.033E7020"

This is a multipart message in MIME format.

------=_NextPart_000_0081_01D32C91.033E7020
Content-Type: text/plain;
charset=
Content-Transfer-Encoding: foobar
MyPlain
------=_NextPart_000_0081_01D32C91.033E7020
Content-Type: text/html;
charset=""
Content-Transfer-Encoding: quoted-printable

MyHtml
------=_NextPart_000_0081_01D32C91.033E7020--

0 comments on commit 4d98eb2

Please sign in to comment.