Skip to content

Commit

Permalink
Merge pull request #100 from sblondon/master
Browse files Browse the repository at this point in the history
Fix attachment parsing when a semicolon character ends the Content-Di…
  • Loading branch information
martinrusev committed Oct 6, 2017
2 parents 7c5a639 + a490081 commit 6e0ee23
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
11 changes: 6 additions & 5 deletions imbox/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,14 @@ def parse_attachment(message_part):
attachment['filename'] = filename

for param in dispositions[1:]:
name, value = decode_param(param)
if param:
name, value = decode_param(param)

if 'file' in name:
attachment['filename'] = value
if 'file' in name:
attachment['filename'] = value

if 'create-date' in name:
attachment['create-date'] = value
if 'create-date' in name:
attachment['create-date'] = value

return attachment

Expand Down
64 changes: 62 additions & 2 deletions tests/parser_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,60 @@
"""


raw_email_with_trailing_semicolon_to_disposition_content = b"""Delivered-To: receiver@example.com
Return-Path: <sender@example.com>
Mime-Version: 1.0
Date: Wed, 22 Mar 2017 15:21:55 -0500
Message-ID: <58D29693.192A.0075.1@wimort.com>
Subject: Re: Reaching Out About Peoples Home Equity
From: sender@example.com
To: receiver@example.com
Content-Type: multipart/alternative; boundary="____NOIBTUQXSYRVOOAFLCHY____"
--____NOIBTUQXSYRVOOAFLCHY____
Content-Type: text/plain; charset=iso-8859-15
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline;
modification-date="Wed, 22 Mar 2017 15:21:55 -0500"
Hello Chloe
--____NOIBTUQXSYRVOOAFLCHY____
Content-Type: multipart/related; boundary="____XTSWHCFJMONXSVGPVDLY____"
--____XTSWHCFJMONXSVGPVDLY____
Content-Type: text/html; charset=iso-8859-15
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline;
modification-date="Wed, 22 Mar 2017 15:21:55 -0500"
<HTML xmlns=3D"http://www.w3.org/1999/xhtml">
<BODY>
<DIV>Hello Chloe</DIV>
</BODY>
</HTML>
--____XTSWHCFJMONXSVGPVDLY____
Content-Type: application/octet-stream; name="abc.xyz"
Content-Description: abc.xyz
Content-Disposition: attachment; filename="abc.xyz";
Content-Transfer-Encoding: base64
R0lGODlhHgHCAPf/AIOPr9GvT7SFcZZjVTEuMLS1tZKUlJN0Znp4eEA7PV1aWvz8+8V6Zl1BNYxX
HvOZ1/zmOd95agUEADs=
--____XTSWHCFJMONXSVGPVDLY____
Content-ID: <VFXVGHAGXNMI.36b3148cbf284ba18d35bdd8386ac266>
Content-Type: image/xxx
Content-Transfer-Encoding: base64
R0lGODlhAQABAPAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==
--____XTSWHCFJMONXSVGPVDLY____--
--____NOIBTUQXSYRVOOAFLCHY____--
"""


class TestParser(unittest.TestCase):

def test_parse_email(self):
Expand Down Expand Up @@ -216,10 +270,16 @@ 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)

# TODO - Complete the test suite
def test_parse_attachment(self):
pass
parsed_email = parse_email(raw_email_with_trailing_semicolon_to_disposition_content)
self.assertEqual(1, len(parsed_email.attachments))
attachment = parsed_email.attachments[0]
self.assertEqual('application/octet-stream', attachment['content-type'])
self.assertEqual(71, attachment['size'])
self.assertEqual('"abc.xyz"', attachment['filename'])
self.assertTrue(attachment['content'])

# TODO - Complete the test suite
def test_decode_mail_header(self):
pass

Expand Down

0 comments on commit 6e0ee23

Please sign in to comment.