"EOF error" when fetching emails in bulk using imap_tools python

475 Views Asked by At

I've build a very simple script that fetches emails from an Outlook inbox. It looks like this:

from imap_tools import MailBox

inbox = MailBox("outlook.office365.com").login("email", "password")

for email in inbox.fetch():  # bulk=True
    print(email)

It's working when I use it like this. Though, when passing the arg bulk=True on the fetch() function it will raise the following error:

Traceback (most recent call last):
  File "/Users/julius/Library/Application Support/JetBrains/IntelliJIdea2022.3/scratches/scratch_1.py", line 5, in <module>
    for email in inbox.fetch(bulk=True):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/imap_tools/mailbox.py", line 171, in fetch
    for fetch_item in (self._fetch_in_bulk if bulk else self._fetch_by_one)(nums, message_parts, reverse):  # noqa
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/imap_tools/mailbox.py", line 144, in _fetch_in_bulk
    fetch_result = self.client.fetch(','.join(message_nums), message_parts)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/imaplib.py", line 548, in fetch
    typ, dat = self._simple_command(name, message_set, message_parts)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/imaplib.py", line 1230, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/imaplib.py", line 1008, in _command
    raise self.abort('socket error: %s' % val)
imaplib.IMAP4.abort: socket error: EOF occurred in violation of protocol (_ssl.c:2396)

Has anyone got a clue on how to fix this?

1

There are 1 best solutions below

0
Vladimir On

Right answer: combine bulk and limit args.

Text from docs: For actions with a large number of messages imap command may be too large and will cause exception at server side, use 'limit' argument for fetch in this case.