Skip to content

Commit

Permalink
Fix return type of message without a preferred body
Browse files Browse the repository at this point in the history
The `get_body(...)` call (and hence the nominal return of the
`get_body_part(...)` method) returns a email.message.EmailMessage. So if
there is no body return an empty such class instead of a string, because
the callers expects that it behaves like a message. E.g.:

      File "alot/db/utils.py", line 499, in extract_body_part
        **{'field_key': 'view'} if body_part.get_content_type() == 'text/plain'
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^
    AttributeError: 'str' object has no attribute 'get_content_type'
  • Loading branch information
johslarsen committed Oct 23, 2024
1 parent 2c32c68 commit 144d6bf
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions alot/db/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def get_body_part(mail, mimetype=None):
:param mail: the mail to use
:type mail: :class:`email.message.EmailMessage`
:returns: The combined text of any parts to be used
:rtype: str
:rtype: :class:`email.message.EmailMessage`
"""

if not mimetype:
Expand All @@ -486,7 +486,9 @@ def get_body_part(mail, mimetype=None):

body_part = mail.get_body(preferencelist)
if body_part is None: # if no part matching preferredlist was found
return ""
empty = email.message.EmailMessage()
empty.set_payload("")
return empty

return body_part

Expand Down

0 comments on commit 144d6bf

Please sign in to comment.