Skip to content

Commit

Permalink
MailAttachment.attachments fix miss forwarded messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ikvk committed Mar 16, 2021
1 parent b957a4f commit 84dbf2b
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ Big thanks to people who helped develop this library:
`nathan30 <https://github.com/nathan30>`_,
`daniel55411 <https://github.com/daniel55411>`_,
`rcarmo <https://github.com/rcarmo>`_,
`bhernacki <https://github.com/bhernacki>`_
`bhernacki <https://github.com/bhernacki>`_,
`ilep <https://github.com/ilep>`_

💰 You may `donate <https://github.com/ikvk/imap_tools/blob/master/docs/donate.rst>`_, if this library helped you.
4 changes: 4 additions & 0 deletions docs/release_notes.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.39.0
======
* Fixed MailAttachment.attachments - message/rfc822 forwarded messages not missing now

0.38.0
======
* Fixed bug at utils.parse_email_addresses - quoted with newlines
Expand Down
2 changes: 1 addition & 1 deletion imap_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
from .folder import MailBoxFolderManager, MailBoxFolderStatusOptions
from .errors import *

__version__ = '0.38.1'
__version__ = '0.39.0'
7 changes: 4 additions & 3 deletions imap_tools/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ def attachments(self) -> ['MailAttachment']:
for part in self.obj.walk():
if part.get_content_maintype() == 'multipart': # multipart/* are containers
continue
if part.get('Content-ID') is None and part.get_filename() is None:
if part.get('Content-ID') is None and part.get_filename() is None \
and part.get_content_type() != 'message/rfc822':
continue
results.append(MailAttachment(part))
return results
Expand All @@ -249,8 +250,8 @@ def filename(self) -> str:
forwarded message (Content-Type = message/rfc822)
:return: filename
"""
filename = self.part.get_filename() or ''
return ''.join(decode_value(*head_part) for head_part in decode_header(filename))
raw = self.part.get_filename() or ''
return ''.join(decode_value(*head_part) for head_part in decode_header(raw))

@property
@lru_cache()
Expand Down
50 changes: 43 additions & 7 deletions tests/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,9 +611,9 @@ def get_content_type(self):
headers={'message-id': ('<5fa9c763>',), 'date': ('Mon, 01 Nov 2020 14:49:07 -0800 (PST)',),
'mime-version': ('1.0',), 'from': ('"Sender" <[email protected]>',),
'to': ('My Name <[email protected]>, Other Name\r\n <[email protected]>',), 'cc': (
'Third Name <[email protected]>, "Quoted Mailing List\r\n One" <[email protected]>, "Quoted\r\n Mailing List Two" <[email protected]>',),
'Third Name <[email protected]>, "Quoted Mailing List\r\n One" <[email protected]>, "Quoted\r\n Mailing List Two" <[email protected]>',),
'bcc': (
'My Name <[email protected]>, Mailing List One - One Name\r\n <[email protected]>, Mailing List Two - Second Name\r\n <[email protected]>',),
'My Name <[email protected]>, Mailing List One - One Name\r\n <[email protected]>, Mailing List Two - Second Name\r\n <[email protected]>',),
'subject': ('Daily',),
'content-type': ('multipart/mixed; boundary=--boundary_20_d4727d16-8454-4fa4-9da0-950a95b2c962',)},
attachments=[
Expand All @@ -630,16 +630,52 @@ def get_content_type(self):
{'email': '[email protected]', 'name': 'Other Name',
'full': 'Other Name <[email protected]>'}),
cc_values=(
{'email': '[email protected]', 'name': 'Third Name', 'full': 'Third Name <[email protected]>'},
{'email': '[email protected]', 'name': 'Quoted Mailing List One',
'full': 'Quoted Mailing List One <[email protected]>'},
{'email': '[email protected]', 'name': 'Quoted Mailing List Two',
'full': 'Quoted Mailing List Two <[email protected]>'}),
{'email': '[email protected]', 'name': 'Third Name', 'full': 'Third Name <[email protected]>'},
{'email': '[email protected]', 'name': 'Quoted Mailing List One',
'full': 'Quoted Mailing List One <[email protected]>'},
{'email': '[email protected]', 'name': 'Quoted Mailing List Two',
'full': 'Quoted Mailing List Two <[email protected]>'}),
bcc_values=({'email': '[email protected]', 'name': 'My Name', 'full': 'My Name <[email protected]>'},
{'email': '[email protected]', 'name': 'Mailing List One - One Name',
'full': 'Mailing List One - One Name <[email protected]>'},
{'email': '[email protected]', 'name': 'Mailing List Two - Second Name',
'full': 'Mailing List Two - Second Name <[email protected]>'}),
reply_to_values=(),
),
'forwarded_message': dict(
subject='статус',
from_='[email protected]',
to=('[email protected]', 'я你Rabea.Bartö[email protected]'),
cc=(),
bcc=(),
reply_to=(),
date=datetime.datetime(2017, 10, 12, 9, 41, 56, tzinfo=datetime.timezone(datetime.timedelta(0, 18000))),
date_str='Thu, 12 Oct 2017 09:41:56 +0500',
text='\r\n',
html='\r\n\r\n <!DOCTYPE html PUBLIC \'-//W3C//DTD XHTML 1.0 Strict//EN\' \'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\'>\r\n <html xmlns=\'http://www.w3.org/1999/xhtml\' lang=\'ru-ru\'\r\n xml:lang=\'en-us\'>\r\n <head>\r\n <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">\r\n \r\n </head>\r\n\r\n <body id=\'body\'>\r\n \r\n \r\n \r\n <a href="http://group.company.ru/ru/order/28922/process/">\r\n \r\n Новый заказ №28922\r\n </a>\r\n\r\n </body>\r\n </html>\r\n\r\n',
headers={'subject': ('=?UTF-8?B?0YHRgtCw0YLRg9GB?=',), 'to': (
'Jessica Schmidt <[email protected]>,\r\n\t=?iso-8859-1?Q?Rabea=2EBart=F6lke=40uni=2Ede?= <\udcd1\udc8f\udce4\udcbd\udca0Rabea.Bart\udcc3\udcb6[email protected]>',),
'from': ('[email protected]',), 'message-id': ('<[email protected]>',),
'date': ('Thu, 12 Oct 2017 09:41:56 +0500',), 'mime-version': ('1.0',),
'in-reply-to': ('<[email protected]>',),
'content-type': ('multipart/mixed;\r\n boundary="------------BF90926EC9DF73443A6B8F28"',),
'content-language': ('ru',)},
attachments=[
dict(
filename='',
content_id='',
content_disposition='attachment',
content_type='message/rfc822',
payload=b'Content-Type: multipart/alternative;\n boundary="===============3693132879591888836=="\nMIME-Version: 1.0\nSubject: =?UTF-8?B?0YHRgtCw0YLRg9GB?= \nFrom: [email protected]\nTo: [email protected]\nDate: Wed, 11 Oct 2017 08:54:32 -0000\nMessage-ID: <[email protected]>\n\n--===============3693132879591888836==\nContent-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\n=D0=9D=D0=BE=D0=B2=D1=8B=D0=B9 =D0=B7=D0=B0=D0=BA=D0=B0=D0=B7 =E2=84=9628=\n922 http://group.company.ru/ru/order/28922/process/\n=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BB:=20\n=D0=9A=D0=BB=D0=B8=D0=B5=D0=BD=D1=82 =D0=9A=D0=BE=D0=B2=D1=87=D0=B5=D0=B3=\n (=D0=93=D1=83=D1=81=D0=B5=D0=B2 =D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=\n)\n\n=D0=A1=D0=B5=D0=B3=D0=BC=D0=B5=D0=BD=D1=82=D1=8B:\n\n- =D0=9C=D0=BE=D1=81=D0=BA=D0=B2=D0=B0 - =D0=A2=D0=B5=D0=BB=D1=8C-=D0=90=D0=\n=B2=D0=B8=D0=B2, some-889, 15.03.2018, =D1=8D=D0=BA=D0=BE=D0=BD=D0=BE=D0=BC=\n: (15 =D0=B2=D0=B7=D1=80.)\n\n- =D0=A2=D0=B5=D0=BB=D1=8C-=D0=90=D0=B2=D0=B8=D0=B2 - =D0=9C=D0=BE=D1=81=D0=\n=BA=D0=B2=D0=B0, some-890, 22.03.2018, =D1=8D=D0=BA=D0=BE=D0=BD=D0=BE=D0=BC=\n: (15 =D0=B2=D0=B7=D1=80.)\n--===============3693132879591888836==\nContent-Type: text/html; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\n\n\n <!DOCTYPE html PUBLIC \'-//W3C//DTD XHTML 1.0 Strict//EN\' \'http://www.=\nw3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\'>\n <html xmlns=3D\'http://www.w3.org/1999/xhtml\' lang=3D\'ru-ru\'\n xml:lang=3D\'en-us\'>\n <head>\n <meta content=3D"text/html; charset=3DUTF-8" http-equiv=3D"Conten=\nt-Type">\n =20\n </head>\n\n <body id=3D\'body\'>\n =20\n =20\n =20\n <a href=3D"http://group.company.ru/ru/order/28922/process/">\n =20\n =D0=9D=D0=BE=D0=B2=D1=8B=D0=B9 =D0=B7=D0=B0=D0=BA=D0=B0=D0=B7=\n =E2=84=9628922\n </a>\n\n </body>\n </html>\n\n\n--===============3693132879591888836==--\n',
),
],
from_values={'email': '[email protected]', 'name': '', 'full': '[email protected]'},
to_values=({'email': '[email protected]', 'name': 'Jessica Schmidt',
'full': 'Jessica Schmidt <[email protected]>'},
{'email': 'я你Rabea.Bartö[email protected]', 'name': 'Rabea.Bartö[email protected]',
'full': 'Rabea.Bartö[email protected] <я你Rabea.Bartö[email protected]>'}),
cc_values=(),
bcc_values=(),
reply_to_values=(),
)
}
88 changes: 88 additions & 0 deletions tests/messages/forwarded_message.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
Subject: =?UTF-8?B?0YHRgtCw0YLRg9GB?=
To: Jessica Schmidt <[email protected]>,
=?iso-8859-1?Q?Rabea=2EBart=F6lke=40uni=2Ede?= <я你Rabea.Bartö[email protected]>
From: [email protected]
Message-ID: <[email protected]>
Date: Thu, 12 Oct 2017 09:41:56 +0500
MIME-Version: 1.0
In-Reply-To: <[email protected]>
Content-Type: multipart/mixed;
boundary="------------BF90926EC9DF73443A6B8F28"
Content-Language: ru

This is a multi-part message in MIME format.
--------------BF90926EC9DF73443A6B8F28
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
--------------BF90926EC9DF73443A6B8F28
Content-Type: message/rfc822;
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
Content-Type: multipart/alternative;
boundary="===============3693132879591888836=="
MIME-Version: 1.0
Subject: =?UTF-8?B?0YHRgtCw0YLRg9GB?=
From: [email protected]
To: [email protected]
Date: Wed, 11 Oct 2017 08:54:32 -0000
Message-ID: <[email protected]>
--===============3693132879591888836==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
=D0=9D=D0=BE=D0=B2=D1=8B=D0=B9 =D0=B7=D0=B0=D0=BA=D0=B0=D0=B7 =E2=84=9628=
922 http://group.company.ru/ru/order/28922/process/
=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BB:=20
=D0=9A=D0=BB=D0=B8=D0=B5=D0=BD=D1=82 =D0=9A=D0=BE=D0=B2=D1=87=D0=B5=D0=B3=
(=D0=93=D1=83=D1=81=D0=B5=D0=B2 =D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=
)
=D0=A1=D0=B5=D0=B3=D0=BC=D0=B5=D0=BD=D1=82=D1=8B:
- =D0=9C=D0=BE=D1=81=D0=BA=D0=B2=D0=B0 - =D0=A2=D0=B5=D0=BB=D1=8C-=D0=90=D0=
=B2=D0=B8=D0=B2, some-889, 15.03.2018, =D1=8D=D0=BA=D0=BE=D0=BD=D0=BE=D0=BC=
: (15 =D0=B2=D0=B7=D1=80.)
- =D0=A2=D0=B5=D0=BB=D1=8C-=D0=90=D0=B2=D0=B8=D0=B2 - =D0=9C=D0=BE=D1=81=D0=
=BA=D0=B2=D0=B0, some-890, 22.03.2018, =D1=8D=D0=BA=D0=BE=D0=BD=D0=BE=D0=BC=
: (15 =D0=B2=D0=B7=D1=80.)
--===============3693132879591888836==
Content-Type: text/html; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable



<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.=
w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns=3D'http://www.w3.org/1999/xhtml' lang=3D'ru-ru'
xml:lang=3D'en-us'>
<head>
<meta content=3D"text/html; charset=3DUTF-8" http-equiv=3D"Conten=
t-Type">
=20
</head>

<body id=3D'body'>
=20
=20
=20
<a href=3D"http://group.company.ru/ru/order/28922/process/">
=20
=D0=9D=D0=BE=D0=B2=D1=8B=D0=B9 =D0=B7=D0=B0=D0=BA=D0=B0=D0=B7=
=E2=84=9628922
</a>

</body>
</html>


--===============3693132879591888836==--

--------------BF90926EC9DF73443A6B8F28--

0 comments on commit 84dbf2b

Please sign in to comment.