martinrusev/imbox

IMAP `ID Command` required by mail providers (Netease, QQ, etc.)

Evanimity opened this issue · 0 comments

For some naughty mail providers, ID Command is required when log in.
Will we plan to add the feature officially?

Scenario

Mail Provider:

  • (Netease) imap.126.com
  • (Netease) imap.163.com

Raised error message when running imbox.messages():

Traceback (most recent call last):
  ...
  File ".../lib/python3.8/site-packages/imbox/imbox.py", line 100, in messages
    return messages_class(connection=self.connection,
  File ".../lib/python3.8/site-packages/imbox/messages.py", line 37, in __init__
    self._uid_list = self._query_uids(**kwargs)
  File ".../lib/python3.8/site-packages/imbox/messages.py", line 48, in _query_uids
    _, data = self.connection.uid('search', None, query_)
  File ".../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

Tested

A little modifications in code will make it work by my test.

Thanks todytttf at (in Chinese) https://www.dytttf.com/2021/11/12/IMAP%E5%8D%8F%E8%AE%AE-%E4%BB%8E%E7%BD%91%E6%98%93%E9%82%AE%E7%AE%B1%E7%9A%84%E9%AA%9A%E6%93%8D%E4%BD%9C%E5%88%B0QQ%E9%82%AE%E7%AE%B1%E7%9A%84BUG/

  1. in imbox.py, on top:
imaplib.Commands['ID'] = 'AUTH'
  1. in imap.py, class ImapTransport, function connect, after IMAP login, before IMAP select:
def connect(self, username, password):
    self.server.login(username, password)

    # START ID Command
    typ, dat = self.server._simple_command('ID', '("name" "example" "version" "1.0.0" "vendor" "example")')
    if typ != 'OK':
        raise Exception(f'ID Not Accepted. Msg: {dat}')
    # END ID Command

    self.server.select()
    logger.debug("Logged into server {} and selected mailbox 'INBOX'"
                            .format(self.hostname))
    return self.server

Suggestions

As mentioned in dytttf's work, it's unclear how an imap server will response if it doesn't support RFC 2971 ID Command. Given poor knowledge on IMAP4, I haven't explored a proper solution to automatically detect if server requires ID.
My suggestion is (maybe) to add a vendor for such providers.

Reference