ezgmail icon indicating copy to clipboard operation
ezgmail copied to clipboard

Emails with attachments produce errors

Open clay4shin opened this issue 3 years ago • 1 comments

Hello, I'm following the book <Automate the Boring Stuff with Python> (Thank you for the amazing book, btw!) for ezgmail module.

I'm having an issue when I search emails with attachments and try to download it. The module seems to be able to search correct emails and save it to a thread object but when I try to read the summary of threads (ezgmail.summary(threads)) or try to access the attachments (threads[0]messages[0].attachments), it's showing me errors.

These are the codes and outputs I received from the terminal:

>>> import ezgmail
>>> threads = ezgmail.search('(now with attachment)')
>>> len(threads)
3
>>> ezgmail.summary(threads)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/shinsooyun/Library/Python/3.9/lib/python/site-packages/ezgmail/__init__.py", line 704, in summary
    (obj.senders(), obj.snippet, obj.latestTimestamp())
  File "/Users/shinsooyun/Library/Python/3.9/lib/python/site-packages/ezgmail/__init__.py", line 120, in senders
    for msg in self.messages:
  File "/Users/shinsooyun/Library/Python/3.9/lib/python/site-packages/ezgmail/__init__.py", line 104, in messages
    self._messages.append(GmailMessage(msg))
  File "/Users/shinsooyun/Library/Python/3.9/lib/python/site-packages/ezgmail/__init__.py", line 273, in __init__
    attachmentId = part["body"]["attachmentId"]
KeyError: 'attachmentId'
>>> threads[0].messages[0].attachments
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/shinsooyun/Library/Python/3.9/lib/python/site-packages/ezgmail/__init__.py", line 107, in messages
    assert len(self._messages) > 0, "GmailThread object has zero messages; please file a new bug report issue: https://github.com/asweigart/ezgmail/issues"
AssertionError: GmailThread object has zero messages; please file a new bug report issue: https://github.com/asweigart/ezgmail/issues

clay4shin avatar Sep 12 '21 21:09 clay4shin

If you are searching for emails with attachments then you are using the wrong gmail search syntax. If you want it to find emails that have attachments to them use threads = ezgmail.search('has:attachment') instead of threads = ezgmail.search('(now with attachment)'). I suggest using the search options, on the right in the search bar (see image below), in gmail to get an accurate search syntax. image

Malafur avatar Oct 21 '21 18:10 Malafur