Skip to content

Can't open e-mails without a body #1538

@mmartin

Description

@mmartin

Describe the bug
I received an e-mail without a body, just an attachment. Alot fails to open it.

Software Versions

  • Python version: 3.8.2
  • Notmuch version: 0.29.3
  • Alot version: master

To Reproduce
Steps to reproduce the behaviour:

  1. try to open an e-mail without a body;
  2. Alot fails to open it.

Error Log

Traceback (most recent call last):
  File "/home/martins/code/alot/alot/ui.py", line 195, in _apply_fire
    await self.apply_commandline(cmdline)
  File "/home/martins/code/alot/alot/ui.py", line 283, in apply_commandline
    self._error_handler(e)
  File "/home/martins/code/alot/alot/ui.py", line 160, in _error_handler
    self.notify(msg, priority='error')
  File "/home/martins/code/alot/alot/ui.py", line 624, in notify
    self.update()
  File "/home/martins/code/alot/alot/ui.py", line 660, in update
    lines.append(self.build_statusbar())
  File "/home/martins/code/alot/alot/ui.py", line 677, in build_statusbar
    info = cb.get_info()
  File "/home/martins/code/alot/alot/buffers/thread.py", line 60, in get_info
    self.get_selected_message().get_mime_part().get_content_type())
AttributeError: 'str' object has no attribute 'get_content_type'

This is due to this function:

alot/alot/db/utils.py

Lines 469 to 491 in 32b885d

def get_body_part(mail, mimetype=None):
"""Returns an EmailMessage.
This consults :ref:`prefer_plaintext <prefer-plaintext>`
to determine if a "text/plain" alternative is preferred over a "text/html"
part.
:param mail: the mail to use
:type mail: :class:`email.message.EmailMessage`
:returns: The combined text of any parts to be used
:rtype: str
"""
if not mimetype:
mimetype = 'plain' if settings.get('prefer_plaintext') else 'html'
preferencelist = {
'plain': ('plain', 'html'), 'html': ('html', 'plain')}[mimetype]
body_part = mail.get_body(preferencelist)
if body_part is None: # if no part matching preferredlist was found
return ""
return body_part

Despite that :rtype: says str, it normally returns email.message.EmailMessage. And when there is no body it returns an empty string.

Which fails in

info['mimetype'] = (
self.get_selected_message().get_mime_part().get_content_type())

via

alot/alot/db/message.py

Lines 277 to 280 in 32b885d

def get_mime_part(self):
if not self._mime_part:
self._mime_part = get_body_part(self.get_email())
return self._mime_part

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions