Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manageable UnexpectedEncodingException #282

Merged
merged 1 commit into from
Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/Message/AbstractPart.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
From: [email protected]
To: [email protected]
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--