alot icon indicating copy to clipboard operation
alot copied to clipboard

make use of new(?) email.Email.Message.get_body()

Open pazz opened this issue 7 years ago • 6 comments

See here. I believe that this was not available when I wrote the extract_bodyfunction to parse a mail into a content string, and we should be able to simplify things.

pazz avatar Jul 11 '18 10:07 pazz

Also a potential alternative: https://pypi.org/project/mail-parser/

pazz avatar Jul 11 '18 10:07 pazz

this sort of works (for most messages). If there's an attachement i still get this.

  File "/home/pazz/projects/alot/alot/buffers/thread.py", line 63, in rebuild
    self._tree = ThreadTree(self.thread)
  File "/home/pazz/projects/alot/alot/widgets/thread.py", line 352, in __init__
    accumulate(msg)
  File "/home/pazz/projects/alot/alot/widgets/thread.py", line 343, in accumulate
    odd = accumulate(reply, odd)
  File "/home/pazz/projects/alot/alot/widgets/thread.py", line 343, in accumulate
    odd = accumulate(reply, odd)
  File "/home/pazz/projects/alot/alot/widgets/thread.py", line 331, in accumulate
    self._message[mid] = MessageTree(msg, odd)
  File "/home/pazz/projects/alot/alot/widgets/thread.py", line 172, in __init__
    self._maintree = SimpleTree(self._assemble_structure())
  File "/home/pazz/projects/alot/alot/widgets/thread.py", line 205, in _assemble_structure
    bodytree = self._get_body()
  File "/home/pazz/projects/alot/alot/widgets/thread.py", line 238, in _get_body
    bodytxt = self._message.accumulate_body()
  File "/home/pazz/projects/alot/alot/db/message.py", line 267, in accumulate_body
    bodytext = extract_body(eml)
  File "/home/pazz/projects/alot/alot/db/utils.py", line 440, in extract_body
    m = mail.get_body()
  File "/usr/lib/python3.6/email/message.py", line 1018, in get_body
    for prio, part in self._find_body(self, preferencelist):
  File "/usr/lib/python3.6/email/message.py", line 989, in _find_body
    yield from self._find_body(subpart, preferencelist)
  File "/usr/lib/python3.6/email/message.py", line 978, in _find_body
    if part.is_attachment():
  File "/usr/lib/python3.6/email/message.py", line 975, in is_attachment
    return False if c_d is None else c_d.content_disposition == 'attachment'
builtins.AttributeError: 'str' object has no attribute 'content_disposition'

I think http://python.flowdas.com/library/email.message.html#email.message.EmailMessage.iter_attachments can help as a replacement for our homebrew stuff to find attachments

pazz avatar Jul 11 '18 11:07 pazz

Interestingly I noticed that our code on python 3.6 will always return an EmailMessage because of our use of Policy objects, I really like this idea, I wonder if it will work better now that we're doing things more in line with the python 3.3+ way.

dcbaker avatar Aug 06 '18 22:08 dcbaker

Any solution on this problem?

pjpjq avatar Jul 26 '19 04:07 pjpjq

This is not a problem but a work in "progress" refactoring request. I did not spend more time on it so far but you are welcome to take it from here..

pazz avatar Jul 26 '19 08:07 pazz

Not sure if it helps in your case but I just had the same issue (builtins.AttributeError: 'str' object has no attribute 'content_disposition') and for me it was solved by specifying the policy argument as polify.default (as it can be seen in the docs, the default value is policy.compat32, i.e. maintaing backwards compatibility to python3.2 https://docs.python.org/3/library/email.parser.html#email.message_from_file)

erogleva avatar Sep 09 '20 08:09 erogleva