I am experiencing an issue with the imaplib library in Python related to the IDLE command. While the IDLE functionality works as expected in my local development environment, it does not work in production.Below is the code, I was using:
async def fetch_top_most_email(user, password):
global mail
try:
# Connect to IMAP server
mail = imaplib.IMAP4_SSL('imappro.zoho.in', 993)
mail.login(user, password)
if not mail:
return False, "Failed to connect to IMAP server"
mail.select('inbox')
# First check for existing unread emails
status, data = mail.search(None, 'UNSEEN')
mail_ids = data[0].split()
if mail_ids:
# Process existing unread emails using your existing logic
result = await process_unread_emails(mail, mail_ids)
if result:
return result
# Enter IDLE mode
print("Entering IDLE mode to wait for new emails...")
tag = str(time.time()).replace('.', '')
mail.send(f'{tag} IDLE\r\n'.encode())
while True:
# Wait for server notifications
response = mail.readline().decode()
if 'EXISTS' in response: # New email arrived
print("New email notification received")
# ✅ Properly exit IDLE mode
# mail.send(b'DONE\r\n')
# response = mail.readline().decode()
# print(f"Exited IDLE: {response}")
# # Check for new unread emails
# status, data = mail.search(None, 'UNSEEN')
# new_mail_ids = data[0].split()
mail.send(b'DONE\r\n')
response = mail.readline().decode()
print(f"Exited IDLE: {response}")
# 🔍 Then perform your search
status, data = mail.search(None, 'UNSEEN')
new_mail_ids = data[0].split()
if new_mail_ids:
result = await process_unread_emails(mail, new_mail_ids)
if result:
return result
elif 'BYE' in response: # Server closed connection
print("Server closed connection")
return False, "Server closed the connection"
# Add a timeout check if needed (e.g., every 29 minutes)
# if time.time() - last_activity > 1740: # 29 minutes
# break
except Exception as e:
print(f"Error in fetch_top_most_email: {e}")
traceback.print_exc()
return False, str(e)