Python imaplib Yahoo error

Want to search folder “SPAM”, for specific_user@any domain, and delete found mail.

Code below …

import imaplib

box = imaplib.IMAP4_SSL('', 993)
typ, data =, 'from','name@*.*')
for num in data[0].split():, '+FLAGS', '\\Deleted')

… is generating these errors below, please assist in debugging, thanks.

Traceback (most recent call last):
  File "C:\Users\Desktop\Desktop\Python Spam Buster\", line 6, in <module>
    typ, data =, 'from','name@*.*')
  File "C:\Users\Desktop\AppData\Local\Programs\Python\Python310\lib\", line 734, in search
    typ, dat = self._simple_command(name, *criteria)
  File "C:\Users\Desktop\AppData\Local\Programs\Python\Python310\lib\", line 1230, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "C:\Users\Desktop\AppData\Local\Programs\Python\Python310\lib\", line 968, in _command
    raise self.error("command %s illegal in state %s, "
imaplib.IMAP4.error: command SEARCH illegal in state AUTH, only allowed in states SELECTED

It’s telling you your not logged in yet and therefore are not allowed to search.

Hi @rpkamp
I must disagree, it’s telling us that there is something wrong with the selected folder (SPAM) and does not process anything after that.
Yahoo Mail server only handles request for INBOX, in a conventional manor. Any other folder request is a mystery!
GMail for example handles a folder request for anything other then INBOX in this manor, ‘[Gmail]/Spam’, but Yahoo Mail server handling anything other then INBOX is a mystery!

How am I sure?

Code below worked perfectly:

import imaplib

box = imaplib.IMAP4_SSL('', 993)
#typ, data =, 'from','name@*.*')
#for num in data[0].split():
#, '+FLAGS', '\\Deleted')

The other thing I know I have incorrectly, is the search wildcard for the domain. I’m pretty sure it’s not @asterisk.asterisk, but no one else appears to know how to do a wildcard search for a domain!!


Got off the phone with Yahoo Premium Support Engineer.

Yahoo mail has a unique way of generating the “Spam” folder filtering system, thus, folder scan would not display it!

Due to the above mentioned, 3rd party mailbox managers, can’t access it by it’s name, SO, an identical box called “Bulk” is generated, giving you access to all the contents of the “Spam” folder via “Bulk”.

When I ran a scan for the folders/boxes, I didn’t notice that I had an extra folder called “Bulk”, that only appears via the scan, and can’t be seen via the web browser.

I’m paraphrasing the above info from the engineer’s explanation, hope it makes sense for anyone having the same issue.


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.