Exception occurs in email module when malformed email stream is provided and get_body function is called. Working with Python 3.13 (debian-trixie). I found this PR: https://github.com/python/cpython/pull/27492/changes but it seems that the issue is still there.
Traceback
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[21], line 1
----> 1 msg = msg.get_body(("plain", "html"))
File /usr/lib/python3.13/email/message.py:1054, in MIMEPart.get_body(self, preferencelist)
1052 best_prio = len(preferencelist)
1053 body = None
-> 1054 for prio, part in self._find_body(self, preferencelist):
1055 if prio < best_prio:
1056 best_prio = prio
File /usr/lib/python3.13/email/message.py:1040, in MIMEPart._find_body(self, part, preferencelist)
1038 candidate = subparts[0] if subparts else None
1039 if candidate is not None:
-> 1040 yield from self._find_body(candidate, preferencelist)
File /usr/lib/python3.13/email/message.py:1040, in MIMEPart._find_body(self, part, preferencelist)
1038 candidate = subparts[0] if subparts else None
1039 if candidate is not None:
-> 1040 yield from self._find_body(candidate, preferencelist)
File /usr/lib/python3.13/email/message.py:1014, in MIMEPart._find_body(self, part, preferencelist)
1013 def _find_body(self, part, preferencelist):
-> 1014 if part.is_attachment():
1015 return
1016 maintype, subtype = part.get_content_type().split('/')
AttributeError: 'str' object has no attribute 'is_attachment'
Minimal reproduction
fromemail.messageimportEmailMessagefromemailimportmessage_from_stringmail_stream= (
"From: sender@example.com\r\n""To: receiver@example.com\r\n""Subject: Malformed\r\n""Content-Type: multipart/related; boundary=OUTER\r\n""\r\n""--OUTER\r\n""Content-Type: multipart/related\r\n""\r\n""raw body no boundary\r\n""--OUTER--"
)
msg=message_from_string(stream, _class=EmailMessage)
msg.get_body(("plain", "html"))
Activity
pythongh-159304: Fix AttributeError in email get_body() for nested ma…
gh-159304: Fix AttributeError in email get_body() for nested malforme…
bitdancer commented on Oct 15, 2026
Thanks. Fix #159306 merged, but it looks like you need to sign the CLA for your github email to get the backports merged.
jul3x commented on Oct 16, 2026
Thank you @bitdancer for quick reaction - I already signed the CLA for the backports.
[3.14] gh-159304: Fix AttributeError in email get_body() for nested m…