Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Yahoo imaplib errors

Linh Lee

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

Code below ...
Python:
import imaplib

box = imaplib.IMAP4_SSL('imap.mail.yahoo.com', 993)
box.login("[email protected]","xxxxxxxxxx")
box.select('SPAM')
typ, data = box.search(None, 'from','name@*.*')
for num in data[0].split():
   box.store(num, '+FLAGS', '\\Deleted')
box.expunge()
box.close()
box.logout()



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

Traceback (most recent call last):
File "C:\Users\Desktop\Desktop\Python Spam Buster\test.py", line 6, in <module>
typ, data = box.search(None, 'from','name@*.*')
File "C:\Users\Desktop\AppData\Local\Programs\Python\Python310\lib\imaplib.py", line 734, in search
typ, dat = self._simple_command(name, *criteria)
File "C:\Users\Desktop\AppData\Local\Programs\Python\Python310\lib\imaplib.py", line 1230, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "C:\Users\Desktop\AppData\Local\Programs\Python\Python310\lib\imaplib.py", 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
 
Have you checked the result of box.select('SPAM') ? I don't know Yahoo mail, but in Gmail the junk folder is named Spam. Perhaps it is case sensitive ?
 
I tried this out with Gmail (don't have Yahoo) and get exactly the same error, see image. As I suspected, it's the select() that failed. Selecting the Spam box doesn't seem to be allowed, even though I see it in my Gmail ! Selecting the Inbox however does work, and returns 2 (as there are 2 emails). However then your search command fails with status BAD. I assume some other syntax is needed, but the imaplib man page is totally schtumm about what criteria can be used with select 😮 Hope this helps some though.

a.jpg
 
As for selecting the Spam box, I got that to work by using the name [Gmail]/Spam as suggested by the output of the box.list command :

Code:
>>> for dir in box.list():
...    print(dir)
...
OK
[b'(\\HasNoChildren) "/" "INBOX"', b'(\\HasChildren) "/" "Personal"', b'(\\HasNoChildren) "/" "Personal/AdSense"', b'(\\HasNoChildren) "/" "Receipts"', b'(\\HasNoChildren) "/" "Unwanted"', b'(\\HasNoChildren) "/" "Work"', b'(\\HasChildren \\Noselect) "/" "[Gmail]"', b'(\\All \\HasNoChildren) "/" "[Gmail]/All Mail"', b'(\\Drafts \\HasNoChildren) "/" "[Gmail]/Drafts"', b'(\\HasNoChildren \\Important) "/" "[Gmail]/Important"', b'(\\HasNoChildren \\Sent) "/" "[Gmail]/Sent Mail"', b'(\\HasNoChildren \\Junk) "/" "[Gmail]/Spam"', b'(\\Flagged \\HasNoChildren) "/" "[Gmail]/Starred"', b'(\\HasNoChildren \\Trash) "/" "[Gmail]/Trash"']
>>> box.select('[Gmail]/Spam')
('OK', [b'0'])
>>>

Maybe that'll work for Yahoo as well...
 
I tried this out with Gmail (don't have Yahoo) and get exactly the same error, see image. As I suspected, it's the select() that failed. Selecting the Spam box doesn't seem to be allowed, even though I see it in my Gmail ! Selecting the Inbox however does work, and returns 2 (as there are 2 emails). However then your search command fails with status BAD. I assume some other syntax is needed, but the imaplib man page is totally schtumm about what criteria can be used with select 😮 Hope this helps some though.

View attachment 1585
GMAIL to search a folder you would use, "[Gmail]/Spam"
 
As for selecting the Spam box, I got that to work by using the name [Gmail]/Spam as suggested by the output of the box.list command :

Code:
>>> for dir in box.list():
...    print(dir)
...
OK
[b'(\\HasNoChildren) "/" "INBOX"', b'(\\HasChildren) "/" "Personal"', b'(\\HasNoChildren) "/" "Personal/AdSense"', b'(\\HasNoChildren) "/" "Receipts"', b'(\\HasNoChildren) "/" "Unwanted"', b'(\\HasNoChildren) "/" "Work"', b'(\\HasChildren \\Noselect) "/" "[Gmail]"', b'(\\All \\HasNoChildren) "/" "[Gmail]/All Mail"', b'(\\Drafts \\HasNoChildren) "/" "[Gmail]/Drafts"', b'(\\HasNoChildren \\Important) "/" "[Gmail]/Important"', b'(\\HasNoChildren \\Sent) "/" "[Gmail]/Sent Mail"', b'(\\HasNoChildren \\Junk) "/" "[Gmail]/Spam"', b'(\\Flagged \\HasNoChildren) "/" "[Gmail]/Starred"', b'(\\HasNoChildren \\Trash) "/" "[Gmail]/Trash"']
>>> box.select('[Gmail]/Spam')
('OK', [b'0'])
>>>

Maybe that'll work for Yahoo as well...
No, tried variations, no doesn't work for Yahoo.
This is truly a Yahoo Mail Server mystery!!
Reddit, there is someone having the same issue as me, finding a way to get Yahoo to accept a folder request, other then INBOX. No one there has any idea!
 
FOLLOWUP:
Through trial and error, I discovered Yahoo Mail Server, handles request for 4 boxes only.
So far I discovered 4 boxes can be selected:
1) INBOX and it does not need to be case sensitive. box.select('inbox')
2) TRASH is case sensitive box.select('Trash')
3) SENT is case sensitive box.select('Sent')
4) ARCHIVE is case sensitive box.select('Archive')

The above are the only folders Yahoo Mail Server accepts a select box request.
 
Last edited:
FOLLOWUP:
Through trial and error, I discovered Yahoo Mail Server, handles request for 4 boxes only.
So far I discovered 4 boxes can be selected:
1) INBOX and it does not need to be case sensitive. box.select('inbox')
2) TRASH is case sensitive box.select('Trash')
3) SENT is case sensitive box.select('Sent')
4) ARCHIVE is case sensitive box.select('Archive')

The above are the only folders Yahoo Mail Server accepts a select box request.
Aha. I was just about to check it out. Very weird that Spam cannot be selected...
 
Sod's luck.... Doesn't seem like anyone has come up with a solution. As you see in these posts, the Spam folder is not even listed when you ask for a list of all folders. It could be a Yahoo design thing. You could try contacting their support (if they have such a thing) ...
 
Looks like Spam doesn't exist according to Yahoo....
But all is not lost ! I read that Yahoo Mail has a setting for automatic cleanup of your Spam folder. In which case I guess you don't really need to write this Python program 😉
 
Looks like Spam doesn't exist according to Yahoo....
But all is not lost ! I read that Yahoo Mail has a setting for automatic cleanup of your Spam folder. In which case I guess you don't really need to write this Python program 😉
Please allow me to explain why I'm going through all this to locate a specific user in the spam folder and delete all emails from them!

I have been bombarded for over 3 months, over 100 emails per day, consistently. User name never changes, "a", but the domain is constantly changing.

Yes, all his emails are hitting the Spam folder, BUT, auto-deleting the entire spam folder can and does purge emails accidentally being sent there! So this is not an option.

This is why I need to target this specific user, wildcard for the domain, and delete his spam:
Code:
 
Last edited:
Yes good point, hadn't thought of that. I also had legitimate mails end up in Spam. You definitely need to ask Yahoo about this. Although chances they will change it seem pretty much nil.
 
Yes good point, hadn't thought of that. I also had legitimate mails end up in Spam. You definitely need to ask Yahoo about this. Although chances they will change it seem pretty much nil.
I agree, but I'll call their support number on Monday. I will definitely return with an update if any!
I appreciate your communication in attempting to resolve this, @cbreemer !!

 
SOLUTION: :blush:
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.
Yeaaaaa FINALLY!!
 
SOLUTION: :blush:
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.
Yeaaaaa FINALLY!!
Yow, that is good news ! Somehow you would not expect the spam folder to be named Bulk. But that's in a name 😉
I'm sure this update will be useful to others over time.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom