diff --git a/imbox/parser.py b/imbox/parser.py index 9d83fe3..bed6886 100644 --- a/imbox/parser.py +++ b/imbox/parser.py @@ -71,7 +71,7 @@ def decode_param(param): if type_ == 'Q': value = quopri.decodestring(code) elif type_ == 'B': - value = base64.decodestring(code) + value = base64.decodebytes(code.encode()) value = str_encode(value, encoding) value_results.append(value) if value_results: diff --git a/tests/parser_tests.py b/tests/parser_tests.py index 7d1fba7..b642d44 100644 --- a/tests/parser_tests.py +++ b/tests/parser_tests.py @@ -82,6 +82,46 @@ Content-Transfer-Encoding: quoted-printable """ +raw_email_encoded_bad_multipart = b"""Delivered-To: receiver@example.com +Return-Path: +From: sender@example.com +To: "Receiver" , "Second\r\n Receiver" +Subject: Re: Looking to connect with you... +Date: Thu, 20 Apr 2017 15:32:52 +0000 +Message-ID: +Content-Type: multipart/related; + boundary="_004_BN6PR16MB179579288933D60C4016D078C31B0BN6PR16MB1795namp_"; + type="multipart/alternative" +MIME-Version: 1.0 +--_004_BN6PR16MB179579288933D60C4016D078C31B0BN6PR16MB1795namp_ +Content-Type: multipart/alternative; + boundary="_000_BN6PR16MB179579288933D60C4016D078C31B0BN6PR16MB1795namp_" +--_000_BN6PR16MB179579288933D60C4016D078C31B0BN6PR16MB1795namp_ +Content-Type: text/plain; charset="utf-8" +Content-Transfer-Encoding: base64 +SGkgRGFuaWVsbGUsDQoNCg0KSSBhY3R1YWxseSBhbSBoYXBweSBpbiBteSBjdXJyZW50IHJvbGUs +Y3J1aXRlciB8IENoYXJsb3R0ZSwgTkMNClNlbnQgdmlhIEhhcHBpZQ0KDQoNCg== +--_000_BN6PR16MB179579288933D60C4016D078C31B0BN6PR16MB1795namp_ +Content-Type: text/html; charset="utf-8" +Content-Transfer-Encoding: base64 +PGh0bWw+DQo8aGVhZD4NCjxtZXRhIGh0dHAtZXF1aXY9IkNvbnRlbnQtVHlwZSIgY29udGVudD0i +CjwvZGl2Pg0KPC9kaXY+DQo8L2JvZHk+DQo8L2h0bWw+DQo= +--_000_BN6PR16MB179579288933D60C4016D078C31B0BN6PR16MB1795namp_-- +--_004_BN6PR16MB179579288933D60C4016D078C31B0BN6PR16MB1795namp_ +Content-Type: image/png; name="=?utf-8?B?T3V0bG9va0Vtb2ppLfCfmIoucG5n?=" +Content-Description: =?utf-8?B?T3V0bG9va0Vtb2ppLfCfmIoucG5n?= +Content-Disposition: inline; + filename="=?utf-8?B?T3V0bG9va0Vtb2ppLfCfmIoucG5n?="; size=488; + creation-date="Thu, 20 Apr 2017 15:32:52 GMT"; + modification-date="Thu, 20 Apr 2017 15:32:52 GMT" +Content-ID: <254962e2-f05c-40d1-aa11-0d34671b056c> +Content-Transfer-Encoding: base64 +iVBORw0KGgoAAAANSUhEUgAAABMAAAATCAYAAAByUDbMAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJ +cvED9AIR3TCAAAMAqh+p+YMVeBQAAAAASUVORK5CYII= +--_004_BN6PR16MB179579288933D60C4016D078C31B0BN6PR16MB1795namp_-- +""" + + class TestParser(unittest.TestCase): def test_parse_email(self): @@ -98,6 +138,10 @@ class TestParser(unittest.TestCase): self.assertEqual('Выписка по карте', parsed_email.subject) self.assertEqual('Выписка по карте 1234', parsed_email.body['html'][0]) + def test_parse_email_bad_multipart(self): + parsed_email = parse_email(raw_email_encoded_bad_multipart) + self.assertEqual("Re: Looking to connect with you...", parsed_email.subject) + def test_parse_email_ignores_header_casing(self): self.assertEqual('one', parse_email('Message-ID: one').message_id) self.assertEqual('one', parse_email('Message-Id: one').message_id)