Imaplib IDLE Not Working in Production Environment (Works in Local but not in production)

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)

What error messages are you getting? Please give us as much context as possible.

There were no errors, but the issue was that real-time notifications were not triggered when new emails arrived, which resulted in the emails not being read. I think IDLE functionality was not working which was not the case in Local Server.

Then it’s most likely not a problem in the code, but in the way the production env. I suppose. (I could be wrong tho, lmk)