zendframework/zend-mail

HeaderWrap::mimeDecodeValue unable to handle multiline utf header splitted across character

fred84 opened this issue · 2 comments

I've encountered email with subject, that was not handled by zend-mail properly. After some investigation I found that header value was split in middle of multibyte utf character.

Test to reproduce it:

    public function testMultilineWithMultibyteSplitAcrossCharacter()
    {
        $originalValue = 'аф';

        $this->assertEquals(strlen($originalValue), 4);

        $part1 = base64_encode(substr($originalValue, 0, 3));
        $part2 = base64_encode(substr($originalValue, 3));

        $header = 'Subject: =?utf-8?B?' . $part1 . '?==?utf-8?B?' . $part2 . '?=';

        $this->assertEquals(
            $originalValue,
            GenericHeader::fromString($header)->getFieldValue()
        );
    }

I fix it locally:

    public static function mimeDecodeValue($value)
    {
        return array_reduce(
            imap_mime_header_decode(imap_utf8($value)),
            function ($acc, $item) { return $acc . $item->text; },
            ''
        );
    }

If depending on imap extension is ok, I will make PR. Or we can check if imap extension is enabled, otherwise use iconv_mime_decode as before.

I think check if imap extension first should be better, and use iconv as fallback

@samsonasik ok, I'll make PR