Skip to content

Commit

Permalink
Support international characters in email addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
ikvk committed Jul 31, 2020
1 parent 05eee21 commit 0ed0775
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 25 deletions.
2 changes: 1 addition & 1 deletion imap_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
from .utils import *
from .errors import *

__version__ = '0.18.1'
__version__ = '0.19.0'
4 changes: 3 additions & 1 deletion imap_tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import inspect
import datetime
from email.utils import getaddresses
from email.header import decode_header
from email.header import decode_header, Header

SHORT_MONTH_NAMES = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')

Expand Down Expand Up @@ -69,6 +69,8 @@ def parse_email_addresses(raw_header: str) -> (dict,):
:return: tuple(dict(name: str, email: str, full: str))
"""
result = []
if type(raw_header) is Header:
raw_header = decode_value(*decode_header(raw_header)[0])
for raw_name, email in getaddresses([raw_header]):
name = decode_value(*decode_header(raw_name)[0]).strip()
email = email.strip()
Expand Down
6 changes: 5 additions & 1 deletion release_notes.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
0.19.0
======
* Support international characters in email addresses

0.18.1
======
* Add deprecated Q to default import, *forgot =)
* Add deprecated Q to default import, *forgot
0.18.0
======
Expand Down
47 changes: 26 additions & 21 deletions tests/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def get_content_type(self):
</body></html>""",
headers={'MIME-Version': ('1.0',), 'From': ('=?utf-8?b?0K/QvdC00LXQutGB?= <[email protected]>',),
'Content-Type': ('multipart/related; boundary="===============1696383123=="',), 'Reply-To': (
'=?UTF-8?B?0L/RgNC40LLQtdGC?= <[email protected]>,\r\n =?UTF-8?B?0L/QvtC60LA=?= <[email protected]>',),
'=?UTF-8?B?0L/RgNC40LLQtdGC?= <[email protected]>,\r\n =?UTF-8?B?0L/QvtC60LA=?= <[email protected]>',),
'Message-Id': ('<[email protected]>',), 'Subject': (
'=?utf-8?b?0KHQvtCx0LXRgNC40YLQtSDQstGB0Y4g0L/QvtGH0YLRgyDQsiDRjdGC0L4=?=\r\n =?utf-8?b?0YIg0Y/RidC40Lo=?=',)},
'=?utf-8?b?0KHQvtCx0LXRgNC40YLQtSDQstGB0Y4g0L/QvtGH0YLRgyDQsiDRjdGC0L4=?=\r\n =?utf-8?b?0YIg0Y/RidC40Lo=?=',)},
attachments=[
dict(
filename='collector.jpg',
Expand Down Expand Up @@ -132,7 +132,7 @@ def get_content_type(self):
'attachment_7bit': dict(
subject='статус',
from_='[email protected]',
to=('[email protected]',),
to=('[email protected]', 'я你Rabea.Bartö[email protected]'),
cc=(),
bcc=(),
reply_to=(),
Expand Down Expand Up @@ -183,25 +183,27 @@ def get_content_type(self):
</html>
""".replace('\n', '\r\n'),
headers={'From': ('[email protected]',), 'X-Spam-Level': ('',),
'X-Virus-Scanned': ('amavisd-new at m101.comp.ru',),
'Message-ID': ('<[email protected]>',),
'Date': ('Thu, 12 Oct 2017 09:41:56 +0500',),
'X-Forwarded-Message-Id': ('<[email protected]>',), 'X-Spam-Flag': ('NO',),
headers={'Message-ID': ('<[email protected]>',),
'References': ('<[email protected]>',), '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]>',),
'Content-Type': ('multipart/mixed;\r\n boundary="------------BF90926EC9DF73443A6B8F28"',),
'Return-Path': ('[email protected]',), 'MIME-Version': ('1.0',), 'X-Spam-Status': (
'No, score=-2.898 required=6.6 tests=[ALL_TRUSTED=-1,\r\n\tBAYES_00=-1.9, HTML_MESSAGE=0.001, URIBL_BLOCKED=0.001]\r\n\tautolearn=ham autolearn_force=no',),
'X-Spam-Score': ('-2.898',), 'From': ('[email protected]',), 'Content-Language': ('ru',),
'Date': ('Thu, 12 Oct 2017 09:41:56 +0500',),
'In-Reply-To': ('<[email protected]>',), 'X-Spam-Flag': ('NO',),
'Return-Path': ('[email protected]',),
'User-Agent': ('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101\r\n Thunderbird/52.4.0',),
'In-Reply-To': ('<[email protected]>',), 'X-Spam-Score': ('-2.898',),
'References': ('<[email protected]>',), 'Content-Language': ('ru',),
'Received': (
'from m101.comp.ru (LHLO m101.comp.ru) (192.168.99.101) by m101.comp.ru\r\n with LMTP; Thu, 12 Oct 2017 09:41:57 +0500 (YEKT)',
'from localhost (localhost [127.0.0.1])\r\n\tby m101.comp.ru (Postfix) with ESMTP id 555275B43;\r\n\tThu, 12 Oct 2017 09:41:57 +0500 (YEKT)',
'from m101.comp.ru ([127.0.0.1])\r\n\tby localhost (m101.comp.ru [127.0.0.1]) (amavisd-new, port 10032)\r\n\twith ESMTP id rY6SAtl4piy1; Thu, 12 Oct 2017 09:41:56 +0500 (YEKT)',
'from localhost (localhost [127.0.0.1])\r\n\tby m101.comp.ru (Postfix) with ESMTP id C8F0F5B4B;\r\n\tThu, 12 Oct 2017 09:41:56 +0500 (YEKT)',
'from m101.comp.ru ([127.0.0.1])\r\n\tby localhost (m101.comp.ru [127.0.0.1]) (amavisd-new, port 10026)\r\n\twith ESMTP id kq2wE_i9_8EK; Thu, 12 Oct 2017 09:41:56 +0500 (YEKT)',
'from [192.168.104.80] (notebook26.hades.company [192.168.104.80])\r\n\tby m101.comp.ru (Postfix) with ESMTPSA id 9563A5B44\r\n\tfor <[email protected]>; Thu, 12 Oct 2017 09:41:56 +0500 (YEKT)'),
'Subject': ('=?UTF-8?B?0YHRgtCw0YLRg9GB?= ',), 'To': ('[email protected]',)},
'X-Forwarded-Message-Id': ('<[email protected]>',),
'X-Virus-Scanned': ('amavisd-new at m101.comp.ru',), 'Subject': ('=?UTF-8?B?0YHRgtCw0YLRg9GB?= ',),
'MIME-Version': ('1.0',), 'Received': (
'from m101.comp.ru (LHLO m101.comp.ru) (192.168.99.101) by m101.comp.ru\r\n with LMTP; Thu, 12 Oct 2017 09:41:57 +0500 (YEKT)',
'from localhost (localhost [127.0.0.1])\r\n\tby m101.comp.ru (Postfix) with ESMTP id 555275B43;\r\n\tThu, 12 Oct 2017 09:41:57 +0500 (YEKT)',
'from m101.comp.ru ([127.0.0.1])\r\n\tby localhost (m101.comp.ru [127.0.0.1]) (amavisd-new, port 10032)\r\n\twith ESMTP id rY6SAtl4piy1; Thu, 12 Oct 2017 09:41:56 +0500 (YEKT)',
'from localhost (localhost [127.0.0.1])\r\n\tby m101.comp.ru (Postfix) with ESMTP id C8F0F5B4B;\r\n\tThu, 12 Oct 2017 09:41:56 +0500 (YEKT)',
'from m101.comp.ru ([127.0.0.1])\r\n\tby localhost (m101.comp.ru [127.0.0.1]) (amavisd-new, port 10026)\r\n\twith ESMTP id kq2wE_i9_8EK; Thu, 12 Oct 2017 09:41:56 +0500 (YEKT)',
'from [192.168.104.80] (notebook26.hades.company [192.168.104.80])\r\n\tby m101.comp.ru (Postfix) with ESMTPSA id 9563A5B44\r\n\tfor <[email protected]>; Thu, 12 Oct 2017 09:41:56 +0500 (YEKT)'),
'X-Spam-Status': (
'No, score=-2.898 required=6.6 tests=[ALL_TRUSTED=-1,\r\n\tBAYES_00=-1.9, HTML_MESSAGE=0.001, URIBL_BLOCKED=0.001]\r\n\tautolearn=ham autolearn_force=no',),
'X-Spam-Level': ('',)},
attachments=[
dict(
filename='Contract 25 04 2020.docx',
Expand All @@ -210,7 +212,10 @@ def get_content_type(self):
),
],
from_values={'email': '[email protected]', 'name': '', 'full': '[email protected]'},
to_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=(),
Expand Down
3 changes: 2 additions & 1 deletion tests/messages/attachment_7bit.eml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ Received: from [192.168.104.80] (notebook26.hades.company [192.168.104.80])
for <[email protected]>; Thu, 12 Oct 2017 09:41:56 +0500 (YEKT)
References: <[email protected]>
Subject: =?UTF-8?B?0YHRgtCw0YLRg9GB?=
To: [email protected]
To: Jessica Schmidt <[email protected]>,
=?iso-8859-1?Q?Rabea=2EBart=F6lke=40uni=2Ede?= <я你Rabea.Bartö[email protected]>
From: [email protected]
X-Forwarded-Message-Id: <[email protected]>
Message-ID: <[email protected]>
Expand Down

0 comments on commit 0ed0775

Please sign in to comment.