This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: AttributeError in email.message.get_body()
Type: behavior Stage: resolved
Components: email Versions: Python 3.11, Python 3.10, Python 3.9
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: andrei.avk, barry, iritkatriel, lukasz.langa, miss-islington, python-dev, r.david.murray, xavier2
Priority: normal Keywords: patch

Created on 2021-01-11 16:45 by xavier2, last changed 2022-04-11 14:59 by admin. This issue is now closed.

File name Uploaded Description Edit
email_bad_formatted.eml xavier2, 2021-01-11 16:45
Pull Requests
URL Status Linked Edit
PR 24192 open python-dev, 2021-01-11 16:49
PR 26903 merged andrei.avk, 2021-06-24 19:11
PR 27492 merged miss-islington, 2021-07-30 17:05
PR 27493 merged miss-islington, 2021-07-30 17:06
Messages (10)
msg384847 - (view) Author: Xavier Hausherr (xavier2) * Date: 2021-01-11 16:45
Following this issue:

Same bug apply to email.message.get_body() with attached email example and the following code: 

        from email.policy import default
        import email

        with open('email_bad_formatted.eml', 'rb') as fp:
            msg = email.message_from_binary_file(fp, policy=default)
        body = msg.get_body()

> Result:
E       AttributeError: 'str' object has no attribute 'is_attachment'

/usr/local/lib/python3.9/email/ AttributeError
msg384848 - (view) Author: Xavier Hausherr (xavier2) * Date: 2021-01-11 16:52
Attached PR fix the issue.
msg393982 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-05-19 22:38
Can you see how it happened that part is a str?
msg393991 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2021-05-20 00:14
Yes, that's the real question.  That's what needs to be fixed, otherwise we'll just keep finding new bugs.  For example, try calling iter_parts() on that message.  It isn't pretty :)
msg393996 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2021-05-20 01:49
Actually, I'm wrong.  The body of a part can be a string, and that's what's going to happen with a malformed body of something claiming to be a multipart. The problem is that there is code that doesn't guard against this possibility.  The following patch against master fixes the bug listed here, as well as iter_parts().  But it causes one test suite failure so it isn't a correct patch as it stands:

diff --git a/Lib/email/ b/Lib/email/
index 3701b30553..d5d4a2385a 100644
--- a/Lib/email/
+++ b/Lib/email/
@@ -982,7 +982,7 @@ def _find_body(self, part, preferencelist):
             if subtype in preferencelist:
                 yield (preferencelist.index(subtype), part)
-        if maintype != 'multipart':
+        if maintype != 'multipart' or not self.is_multipart():
         if subtype != 'related':
             for subpart in part.iter_parts():
@@ -1087,7 +1087,7 @@ def iter_parts(self):
         Return an empty iterator for a non-multipart.
-        if self.get_content_maintype() == 'multipart':
+        if self.is_multipart():
             yield from self.get_payload()
     def get_content(self, *args, content_manager=None, **kw):

Maybe someone can take this and finish it (with tests)...I may or may not have time to get back to this.
msg396497 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-06-24 16:13
I'll try to fix the unit test today.
msg396506 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-06-24 19:17
Put up the PR here:
msg398574 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-07-30 17:05
New changeset e3f877c32d7cccb734f45310f26beeec793364ce by andrei kulakov in branch 'main':
bpo-42892: fix email multipart attribute error (GH-26903)
msg398585 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-07-30 17:27
New changeset 6f950023c6a2168b229363d75f59a24ecdd66d19 by Miss Islington (bot) in branch '3.10':
bpo-42892: fix email multipart attribute error (GH-26903) (GH-27492)
msg398587 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-07-30 17:31
New changeset 0f42b726c87f72d522893f927b4cb592b8875641 by Miss Islington (bot) in branch '3.9':
bpo-42892: fix email multipart attribute error (GH-26903) (GH-27493)
Date User Action Args
2022-04-11 14:59:40adminsetgithub: 87058
2021-07-30 17:31:19lukasz.langasetstatus: open -> closed
type: behavior
resolution: fixed
stage: patch review -> resolved
2021-07-30 17:31:02lukasz.langasetmessages: + msg398587
2021-07-30 17:27:14lukasz.langasetmessages: + msg398585
2021-07-30 17:06:04miss-islingtonsetpull_requests: + pull_request26009
2021-07-30 17:05:58miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request26008
2021-07-30 17:05:57lukasz.langasetnosy: + lukasz.langa
messages: + msg398574
2021-06-28 09:31:12iritkatrielsetversions: + Python 3.10, Python 3.11
2021-06-24 19:17:44andrei.avksetmessages: + msg396506
2021-06-24 19:11:30andrei.avksetpull_requests: + pull_request25479
2021-06-24 16:13:42andrei.avksetnosy: + andrei.avk
messages: + msg396497
2021-05-20 01:49:38r.david.murraysetmessages: + msg393996
2021-05-20 00:14:26r.david.murraysetmessages: + msg393991
2021-05-19 22:38:48iritkatrielsetmessages: + msg393982
2021-01-11 16:52:14xavier2setmessages: + msg384848
2021-01-11 16:49:48python-devsetkeywords: + patch
nosy: + python-dev

pull_requests: + pull_request23019
stage: patch review
2021-01-11 16:45:57xavier2create