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.

classification
Title: email.message.EmailMessage.get_body
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: andrei.avk, kj, patrick totzke
Priority: normal Keywords:

Created on 2020-06-17 16:06 by patrick totzke, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
msg patrick totzke, 2020-06-17 16:06 spam email that triggers the bug
Messages (2)
msg371755 - (view) Author: patrick totzke (patrick totzke) * Date: 2020-06-17 16:06
I am trying to use EmailMessage.get_body() on the attached spam email.
Although that message may be malformed, I believe that this method should fail gracefully.

To reproduce

```
with open('msg', 'rb') as f: 
    m = email.message_from_binary_file(f, _class=email.message.EmailMessage) 

 m.get_body()                                                                                               
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-21-0d3554e3ba7a> in <module>
----> 1 m.get_body()

/usr/lib/python3.8/email/message.py in get_body(self, preferencelist)
   1016         best_prio = len(preferencelist)
   1017         body = None
-> 1018         for prio, part in self._find_body(self, preferencelist):
   1019             if prio < best_prio:
   1020                 best_prio = prio

/usr/lib/python3.8/email/message.py in _find_body(self, part, preferencelist)
    987         if subtype != 'related':
    988             for subpart in part.iter_parts():
--> 989                 yield from self._find_body(subpart, preferencelist)
    990             return
    991         if 'related' in preferencelist:

/usr/lib/python3.8/email/message.py in _find_body(self, part, preferencelist)
    987         if subtype != 'related':
    988             for subpart in part.iter_parts():
--> 989                 yield from self._find_body(subpart, preferencelist)
    990             return
    991         if 'related' in preferencelist:

/usr/lib/python3.8/email/message.py in _find_body(self, part, preferencelist)
    976 
    977     def _find_body(self, part, preferencelist):
--> 978         if part.is_attachment():
    979             return
    980         maintype, subtype = part.get_content_type().split('/')

AttributeError: 'str' object has no attribute 'is_attachment'

```

I am on Python 3.8.3 on debian testing.
msg399605 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-08-15 02:24
This issue was fixed by https://github.com/python/cpython/pull/26903
(I confirmed that the error no longer happens with attached file).

This can be closed as fixed.
History
Date User Action Args
2022-04-11 14:59:32adminsetgithub: 85182
2021-09-04 04:12:11andrei.avksetstatus: open -> closed

nosy: + kj
versions: + Python 3.11, - Python 3.8
resolution: fixed
type: crash -> behavior
stage: resolved
2021-08-15 02:24:53andrei.avksetnosy: + andrei.avk
messages: + msg399605
2020-06-17 16:06:53patrick totzkecreate