Skip to content

Commit 329fe0f

Browse files
committed
+MailAttachment.__str__, fix MailMessage.text,html
1 parent 33da329 commit 329fe0f

99 files changed

Lines changed: 137 additions & 114 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ High level lib for work with email by IMAP:
2020
.. image:: https://img.shields.io/pypi/dm/imap_tools.svg?style=social
2121

2222
=============== ================================================================================================
23-
Python version 3.5+
23+
Python version 3.8+
2424
License Apache-2.0
2525
PyPI https://pypi.python.org/pypi/imap_tools/
2626
RFC `IMAP4.1 <https://tools.ietf.org/html/rfc3501>`_,

docs/release_notes.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
1.9.0
2+
=====
3+
* Added: __str__ to MailAttachment
4+
* Fixed: MailMessage.text parser - text with inline attachments case
5+
* Fixed: MailMessage.html parser - html with inline attachments case
6+
* Dropped: support py3.3,py3.4,py3.5,py3.6,py3.7
7+
18
1.8.0
29
=====
310
* Added: BaseMailBox.numbers_to_uids - Get message uids by message numbers

imap_tools/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
from .utils import EmailAddress
1212
from .errors import *
1313

14-
__version__ = '1.8.0'
14+
__version__ = '1.9.0'

imap_tools/consts.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import re
2+
import sys
23

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

56
UID_PATTERN = re.compile(r'(^|\s+|\W)UID\s+(?P<uid>\d+)')
67

78
CODECS_OFFICIAL_REPLACEMENT_CHAR = '�'
89

10+
PYTHON_VERSION_MINOR = int(sys.version_info.minor)
11+
912

1013
class MailMessageFlags:
1114
"""

imap_tools/mailbox.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import re
2-
import sys
32
import imaplib
43
import datetime
54
from collections import UserString
@@ -8,7 +7,7 @@
87
from .message import MailMessage
98
from .folder import MailBoxFolderManager
109
from .idle import IdleManager
11-
from .consts import UID_PATTERN
10+
from .consts import UID_PATTERN, PYTHON_VERSION_MINOR
1211
from .utils import clean_uids, check_command_status, chunks, encode_folder, clean_flags, check_timeout_arg_support, \
1312
chunks_crop
1413
from .errors import MailboxStarttlsError, MailboxLoginError, MailboxLogoutError, MailboxNumbersError, \
@@ -21,8 +20,6 @@
2120

2221
Criteria = Union[AnyStr, UserString]
2322

24-
PYTHON_VERSION_MINOR = sys.version_info.minor
25-
2623

2724
class BaseMailBox:
2825
"""Working with the email box"""

imap_tools/message.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,24 +178,26 @@ def date(self) -> datetime.datetime:
178178
@lru_cache()
179179
def text(self) -> str:
180180
"""Plain text of the mail message"""
181+
results = []
181182
for part in self.obj.walk():
182183
if part.get_content_maintype() == 'multipart' or part.get_filename():
183184
continue
184185
if part.get_content_type() in ('text/plain', 'text/'):
185-
return decode_value(part.get_payload(decode=True), part.get_content_charset())
186-
return ''
186+
results.append(decode_value(part.get_payload(decode=True), part.get_content_charset()))
187+
return ''.join(results)
187188

188189
@property
189190
@lru_cache()
190191
def html(self) -> str:
191192
"""HTML text of the mail message"""
193+
results = []
192194
for part in self.obj.walk():
193195
if part.get_content_maintype() == 'multipart' or part.get_filename():
194196
continue
195197
if part.get_content_type() == 'text/html':
196198
html = decode_value(part.get_payload(decode=True), part.get_content_charset())
197-
return replace_html_ct_charset(html, 'utf-8')
198-
return ''
199+
results.append(replace_html_ct_charset(html, 'utf-8'))
200+
return ''.join(results)
199201

200202
@property
201203
@lru_cache()
@@ -233,6 +235,9 @@ class MailAttachment:
233235
def __init__(self, part):
234236
self.part = part
235237

238+
def __str__(self):
239+
return '<{} | {} | {} | {}>'.format(self.filename, self.content_type, self.content_disposition, self.content_id)
240+
236241
@property
237242
@lru_cache()
238243
def filename(self) -> str:

imap_tools/query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def __init__(
8686
all: Optional[bool] = None, # noqa
8787
uid: Optional[Union[str, Iterable[str], UidRange]] = None,
8888
header: Optional[Union[Header, List[Header]]] = None,
89-
gmail_label: Optional[Union[str, List[str]]] = None): # todo newline after drop 3.5
89+
gmail_label: Optional[Union[str, List[str]]] = None):
9090
self.converted_strings = converted_strings
9191
for val in converted_strings:
9292
if not any(isinstance(val, t) for t in (str, UserString)):

imap_tools/utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,13 @@ def replace_html_ct_charset(html: str, new_charset: str) -> str:
205205
meta_ct_match = re.search(r'<\s*meta .*?content-type.*?>', html, re.IGNORECASE | re.DOTALL)
206206
if meta_ct_match:
207207
meta = meta_ct_match.group(0)
208-
meta_new = re.sub(r'charset\s*=\s*[a-zA-Z0-9_:.+-]+', 'charset={}'.format(new_charset), meta, 1, re.IGNORECASE)
208+
meta_new = re.sub(
209+
pattern=r'charset\s*=\s*[a-zA-Z0-9_:.+-]+',
210+
repl='charset={}'.format(new_charset),
211+
string=meta,
212+
count=1,
213+
flags=re.IGNORECASE
214+
)
209215
html = html.replace(meta, meta_new)
210216
return html
211217

tests/messages_data/address_quoted_newlines.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
cc=('third.name@domain.com', 'quoted-mailing-list-one@domain.com', 'quoted-mailing-list-two@domain.com'),
99
bcc=('my.name@domain.com', 'list1-one-name@domain.com', 'list2-second-name@domain.com'),
1010
reply_to=(),
11-
date=datetime.datetime(2020, 11, 1, 14, 49, 7, tzinfo=datetime.timezone(datetime.timedelta(-1, 57600))),
11+
date=datetime.datetime(2020, 11, 1, 14, 49, 7, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=57600))),
1212
date_str='Mon, 01 Nov 2020 14:49:07 -0800 (PST)',
1313
text='Daily Data: D09.ZPH (Averaged data)\nEmail generated: 10/11/2020 00:04:03.765\nEmail sent: 10/11/2020 00:49:03.125',
1414
html='',

tests/messages_data/att_name_in_content_type.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
cc=(),
99
bcc=(),
1010
reply_to=(),
11-
date=datetime.datetime(2020, 11, 9, 14, 49, 7, tzinfo=datetime.timezone(datetime.timedelta(-1, 57600))),
11+
date=datetime.datetime(2020, 11, 9, 14, 49, 7, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=57600))),
1212
date_str='Mon, 09 Nov 2020 14:49:07 -0800 (PST)',
1313
text='Daily Data: D09.ZPH (Averaged data)\nEmail generated: 10/11/2020 00:04:03.765\nEmail sent: 10/11/2020 00:49:03.125',
1414
html='',

0 commit comments

Comments
 (0)