Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Lib/email/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,16 @@ def iter_attachments(self):
maintype, subtype = self.get_content_type().split('/')
if maintype != 'multipart' or subtype == 'alternative':
return
parts = self.get_payload().copy()
payload = self.get_payload()
# Certain malformed messages can have content type set to `multipart/*`
# but still have single part body, in which case payload.copy() can
# fail with AttributeError.
try:
parts = payload.copy()
except AttributeError:
# payload is not a list, it is most probably a string.
return

if maintype == 'multipart' and subtype == 'related':
# For related, we treat everything but the root as an attachment.
# The root may be indicated by 'start'; if there's no start or we
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_email/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,15 @@ def test_set_content_does_not_add_MIME_Version(self):
m.set_content(content_manager=cm)
self.assertNotIn('MIME-Version', m)

def test_string_payload_with_multipart_content_type(self):
msg = message_from_string(textwrap.dedent("""\
Content-Type: multipart/mixed; charset="utf-8"

sample text
"""), policy=policy.default)
attachments = msg.iter_attachments()
self.assertEqual(list(attachments), [])


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Email with single part but content-type set to ``multipart/*`` doesn't raise
AttributeError anymore.