Skip to content

Commit

Permalink
fix MailAttachment.content_id enc
Browse files Browse the repository at this point in the history
  • Loading branch information
ikvk committed Nov 2, 2024
1 parent 3b8161a commit 3e4f914
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,8 @@ Big thanks to people who helped develop this library:
`sapristi <https://github.com/sapristi>`_,
`thomwiggers <https://github.com/thomwiggers>`_,
`histogal <https://github.com/histogal>`_,
`K900 <https://github.com/K900>`_
`K900 <https://github.com/K900>`_,
`homoLudenus <https://github.com/homoLudenus>`_

Help the project
----------------
Expand Down
4 changes: 4 additions & 0 deletions docs/release_notes.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.7.4
=====
* Fixed: encoding bug at MailAttachment.content_id

1.7.3
=====
* Fixed: bug in 3.12.6+ after [[3.12] [CVE-2023-27043] gh-102988: Reject malformed addresses in email.parseaddr()]
Expand Down
2 changes: 1 addition & 1 deletion imap_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
from .utils import EmailAddress
from .errors import *

__version__ = '1.7.3'
__version__ = '1.7.4'
5 changes: 4 additions & 1 deletion imap_tools/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,10 @@ def filename(self) -> str:
@property
@lru_cache()
def content_id(self) -> str:
return self.part.get('Content-ID', '').lstrip('<').rstrip('>')
if 'Content-ID' in self.part:
raw = self.part['Content-ID']
return ''.join(decode_value(*head_part) for head_part in decode_header(raw)).lstrip('<').rstrip('>')
return ''

@property
@lru_cache()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
From: [email protected]
To: [email protected]
Subject: Пример email с вложением и Content-ID
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="boundary123"

--boundary123
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit
текстовое сообщение с вложением, у которого есть Content-ID.
--boundary123
Content-Type: image/png; name="example.png"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="example.png"
Content-ID: Test0 =?UTF-8?B?Iua8ouWtlyI=?= mid1 =?UTF-8?B?Iua8ouWtlyI=?= tail2
iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAAFQAAABN3x3XAAAAAElFTkSuQmCC
--boundary123--
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import datetime
from imap_tools import EmailAddress

DATA = dict(
subject='Пример email с вложением и Content-ID',
from_='[email protected]',
to=('[email protected]',),
cc=(),
bcc=(),
reply_to=(),
date=datetime.datetime(1900, 1, 1, 0, 0),
date_str='',
text='текстовое сообщение с вложением, у которого есть Content-ID.\r\n',
html='',
headers={'from': ('[email protected]',), 'to': ('[email protected]',), 'subject': ('\udcd0\udc9f\udcd1\udc80\udcd0\udcb8\udcd0\udcbc\udcd0\udcb5\udcd1\udc80 email \udcd1\udc81 \udcd0\udcb2\udcd0\udcbb\udcd0\udcbe\udcd0\udcb6\udcd0\udcb5\udcd0\udcbd\udcd0\udcb8\udcd0\udcb5\udcd0\udcbc \udcd0\udcb8 Content-ID',), 'mime-version': ('1.0',), 'content-type': ('multipart/mixed; boundary="boundary123"',)},
attachments=[
dict(
filename='example.png',
content_id='Test0 "漢字" mid1 "漢字" tail2',
content_disposition='attachment',
content_type='image/png',
payload=b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x05\x00\x00\x00\x00\x15\x00\x00\x00\x13w\xc7u\xc0\x00\x00\x00\x12QS\x91+\x90\x98 ',
),
],
from_values=EmailAddress(name='', email='[email protected]'),
to_values=(EmailAddress(name='', email='[email protected]'),),
cc_values=(),
bcc_values=(),
reply_to_values=(),
)

0 comments on commit 3e4f914

Please sign in to comment.