jamesridgway / attachment-downloader

Simple tool for downloading email attachments for all emails in a given folder using an IMAP client
MIT License
85 stars 39 forks source link

Gmail: "command SEARCH illegal in state AUTH, only allowed in states SELECTED" #42

Closed dicarsio closed 1 year ago

dicarsio commented 1 year ago

Gmail seems to be erroring when we define an imap folder that does not exist. Maybe check if the folder exists and return a clearer error?

Traceback (most recent call last):
  File "/var/services/homes/dicarsio/.local/bin/attachment-downloader", line 113, in <module>
    messages = mail.messages(**filter_options)
  File "/var/services/homes/dicarsio/.local/lib/python3.8/site-packages/imbox/imbox.py", line 100, in messages
    return messages_class(connection=self.connection,
  File "/var/services/homes/dicarsio/.local/lib/python3.8/site-packages/imbox/vendors/gmail.py", line 39, in __init__
    super().__init__(connection, parser_policy, **kwargs)
  File "/var/services/homes/dicarsio/.local/lib/python3.8/site-packages/imbox/messages.py", line 37, in __init__
    self._uid_list = self._query_uids(**kwargs)
  File "/var/services/homes/dicarsio/.local/lib/python3.8/site-packages/imbox/messages.py", line 48, in _query_uids
    _, data = self.connection.uid('search', None, query_)
  File "/usr/lib/python3.8/imaplib.py", line 876, in uid
    raise self.error("command %s illegal in state %s, "
imaplib.error: command SEARCH illegal in state AUTH, only allowed in states SELECTED

Sample command:

attachment-downloader --host=$EMAIL_HOST --username=$EMAIL_USERNAME --password=$EMAIL_PASSWORD \
    --imap-folder=$EMAIL_IMAP_FOLDER --output=$EMAIL_DEST_FOLDER --date-after=$START_DATE --date-before=$END_DATE \
    --filename-template="{{ date.strftime('%Y%m%d %H%M%S') }} {{ attachment_name }}"

Thanks,

C.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

hilocz commented 1 year ago

Have same problem, it's general problem, because I got this error against GroupWise IMAP server

jamesridgway commented 1 year ago

I believe the gmail related issue is a bit more of a Gmail specific thing.

A fix should be available in 1.2.4. @dicarsio can you confirm if this fixes your issue please?

thaasoph commented 1 year ago

TL;DR: Check your folder name and make sure your mail provider doesn't prefix the folder names with anything. In my case, all my folders had the prefix "INBOX."

I had the same problem but it wasn't a bug, the error message was just misleading. In my case the names of the folders I created in my IMAP mailbox were all prefixed with "INBOX.". So, if I created the folder "scans" it's actual name was "INBOX.scans".

If you are using an unknown folder name with Imbox (the module used by this library for IMAP) it throws this error message. By setting my parameter --imap-folder correctly, with the proper prefix, everything worked like a charm.

I figured out the prefix used by my provider simply by printing all of the folders found by Imbox like this:

from imbox import Imbox
import ssl

mail = Imbox("example.com",
                 username="mail@example.com",
                 password="123456",
                 port=993,
                 ssl=True,
                 ssl_context=ssl.create_default_context(),
                 starttls=False)

print(mail.folders())

There is an open issue at Imbox that suggests to compare the supplied folder name with the folders returned by the inbox in order to print a more accurate error message: https://github.com/martinrusev/imbox/issues/38

Not a difficult thing to do but I haven't gotten around to making a pull request to fix it in either repository.

jamesridgway commented 1 year ago

Thanks for your feedback @thaasoph

1.2.4 includes better logging that will log out the folder names as they apear in Imbox - https://github.com/jamesridgway/attachment-downloader/blob/master/bin/attachment-downloader#L183

As this doesn't appear to be an actual bug as such I'll close this issue