Skip to content

Commit

Permalink
Merge pull request #230 from rmlt/fix_message_rfc822_attachments__bas…
Browse files Browse the repository at this point in the history
…ed_on_master

Fix message/rfc822 attachment processing
  • Loading branch information
Pietro395 authored Dec 19, 2023
2 parents 8c664a4 + 5bd961f commit 997bed0
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
22 changes: 20 additions & 2 deletions django_mailbox/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,10 @@ def _get_dehydrated_message(self, msg, record):
settings = utils.get_settings()

new = EmailMessage()
if msg.is_multipart():
if (
msg.is_multipart()
and not 'attachment' in msg.get('Content-Disposition', '')
):
for header, value in msg.items():
new[header] = value
for part in msg.get_payload():
Expand Down Expand Up @@ -339,11 +342,21 @@ def _get_dehydrated_message(self, msg, record):

attachment = MessageAttachment()

if msg.get_content_type() == 'message/rfc822':
attachment_payloads = msg.get_payload()
if len(attachment_payloads) != 1:
raise AssertionError(
"Attachment of type 'message/rfc822' "
'must have exactly 1 payload.'
)
attachment_payload = attachment_payloads[0].as_bytes()
else:
attachment_payload = msg.get_payload(decode=True)
attachment.document.save(
uuid.uuid4().hex + extension,
ContentFile(
BytesIO(
msg.get_payload(decode=True)
attachment_payload
).getvalue()
)
)
Expand Down Expand Up @@ -832,6 +845,11 @@ def _get_rehydrated_headers(self):
return email.message_from_string(headers)

def _set_dehydrated_headers(self, email_object):
if email_object._payload is None:
# otherwise it breaks in
# lib/python3.x/email/generator.py:_handle_message,
# line: "self._fp.write(payload)"
email_object._payload = ""
self.headers = email_object.as_string()

def __delitem__(self, name):
Expand Down
62 changes: 62 additions & 0 deletions django_mailbox/tests/messages/message_with_rfc822_attachment.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
From: Adam Coddington <[email protected]>
To: Adam Coddington <[email protected]>
Subject: Email with RFC 822 attachment
Date: Fri, 15 May 2020 11:15:00 +0000
Message-ID: <[email protected]>
Content-Type: multipart/mixed;
boundary="_004_ce1f843c1a2d4de7a0c7c78c84a50f8dadamcoddington.net_"
Return-Path: [email protected]
MIME-Version: 1.0

--_004_ce1f843c1a2d4de7a0c7c78c84a50f8dadamcoddington.net_
Content-Type: multipart/alternative;
boundary="_000_ce1f843c1a2d4de7a0c7c78c84a50f8dadamcoddington.net_"
--_000_ce1f843c1a2d4de7a0c7c78c84a50f8dadamcoddington.net_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Dear Sir or Madam,
I am sending you an email message as an attachment.
--_000_ce1f843c1a2d4de7a0c7c78c84a50f8dadamcoddington.net_
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<html xmlns=3D"http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Diso-8859-=
1">
</head>
<body>
<div>
<p>Dear Sir or Madam,</p>
<p>I am sending you an email message as an attachment.</p>
</div>
</body>
</html>

--_000_ce1f843c1a2d4de7a0c7c78c84a50f8dadamcoddington.net_--

--_004_ce1f843c1a2d4de7a0c7c78c84a50f8dadamcoddington.net_
Content-Type: message/rfc822
Content-Disposition: attachment;
creation-date="Fri, 15 May 2020 11:10:00 GMT";
modification-date="Fri, 15 May 2020 11:10:00 GMT"
From: Adam Coddington <[email protected]>
To: Adam Coddington <[email protected]>
Subject: Attached email
Date: Fri, 15 May 2020 09:00:00 +0000
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0
Hello!
This is the attached email.
--_004_ce1f843c1a2d4de7a0c7c78c84a50f8dadamcoddington.net_--
17 changes: 17 additions & 0 deletions django_mailbox/tests/test_process_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ def test_message_with_attachments(self):
'heart.png',
)

def test_message_with_rfc822_attachment(self):
message = self._get_email_object('message_with_rfc822_attachment.eml')

mailbox = Mailbox.objects.create()
msg = mailbox.process_incoming_message(message)

expected_count = 1
actual_count = msg.attachments.count()

self.assertEqual(
expected_count,
actual_count,
)

attachment = msg.attachments.all()[0]
self.assertIsNone(attachment.get_filename())

def test_message_with_utf8_attachment_header(self):
""" Ensure that we properly handle UTF-8 encoded attachment
Expand Down

0 comments on commit 997bed0

Please sign in to comment.