python - imaplib.error: command SEARCH illegal in state AUTH, only allowed in states SELECTED -


def connect_imap():     m = imaplib.imap4_ssl("imap.gmail.com", 993)     print("{0} connecting mailbox via imap...".format(datetime.datetime.today().strftime("%y-%m-%d %h:%m:%s")))     details = login_credentials()     m.login(details[0], details[1])     return m   m = connect_imap() typ, data = m.search(none, 'all') m.close() m.logout() 

the output of above code is:

2016-08-24 10:55:34 connecting mailbox via imap...     traceback (most recent call last):       file "/home/zoikmail/desktop/test.py", line 25, in <module>         typ, data = m.search(none, 'all')       file "/usr/lib/python2.7/imaplib.py", line 640, in search         typ, dat = self._simple_command(name, *criteria)       file "/usr/lib/python2.7/imaplib.py", line 1088, in _simple_command         return self._command_complete(name, self._command(name, *args))       file "/usr/lib/python2.7/imaplib.py", line 838, in _command         ', '.join(commands[name])))     imaplib.error: command search illegal in state auth, allowed in states selected     [finished in 1.2s exit code 1]     [shell_cmd: python -u "/home/zoikmail/desktop/test.py"]     [dir: /home/zoikmail/desktop]     [path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games] 

what's wrong in above code?

you need select mailbox after connecting imap-server. use

m.select() 

after connecting , before search.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -