Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Evanimity opened this issue Apr 27, 2022 · 0 comments
Open

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

Evanimity opened this issue Apr 27, 2022 · 0 comments

Comments

@Evanimity
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant