Skip to content

AttributeError: 'str' object has no attribute 'is_attachment' in email module when calling get_body #159304

Open

Description

@jul3x

Bug report

Bug

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: http://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

from email.message import EmailMessage
from email import message_from_string

mail_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"))

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Oct 13, 2026
  2. added a commit that references this issue on Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions