I am trying to check for new emails in real time. I believe the code below should work but I get an "AttributeError: Unknown IMAP4 command: 'idle'" error. Can anyone see what the issue is?
import imaplib
import email
username = "[email protected]"
password = "xxxxx"
# Connect to the IMAP server
mail = imaplib.IMAP4_SSL('mail.xxxxx.com')
# Login to the server
mail.login(username, password)
# Select the INBOX folder
mail.select("INBOX")
# Start listening for new emails
mail.idle()
Updated answer:
imaplibdoesn't supportIDLE. Github issue (from 2011!) for this is still open --> https://github.com/python/cpython/issues/55454It provides several solutions for this, so I'm not going to reinvent the wheel here (pun intended).
Old answer
I'm going to guess that your specific email server doesn't support the
IDLEcommand.Quick recap here for the
IDLEcommand, and note that this is an optional feature.You can check this using the code below (login to server not needed to check capabilities):
This will output a
tuple[str]which should look like this:And note that in the response that I got (from GMail) includes the
IDLEcapability...