Skip to content

IMAPClient IDLE Command Skipping Email Notifications #611

@AbdulrhmanSayedAli

Description

@AbdulrhmanSayedAli

I am using the IMAPClient library to implement an email notifier using the IMAP IDLE command to wait for incoming email notifications. However, the IDLE command seems to be skipping some email notifications when multiple emails are sent in quick succession.

For example, when I send 3 emails in a row, the code only receives a notification for the first one. After the first notification, it seems to miss the others, even though they arrive in the inbox.

Here’s the relevant code snippet:

IDLE_TIMEOUT = 15 * 60
def email_notifier():
    with IMAPClient(host=HOST) as client:
        client.login(USERNAME, PASSWORD)
        client.select_folder(MAILBOX)

        while True:
            try:
                # Wait for up to {IDLE_TIMEOUT} seconds for an IDLE response
                client.idle()

                responses = client.idle_check(timeout=IDLE_TIMEOUT)
                if not responses:
                    print("Server sent: nothing")
                else:
                    print("Server sent:", responses)
                    new_emails_orders = []
                    for response in responses:
                        if response[1].decode() == "EXISTS":
                            new_emails_orders.append(response[0])
                    if new_emails_orders:
                        task_queue.put(new_emails_orders)
                client.idle_done()
            except KeyboardInterrupt:
                print("Shutting down.")
                break

        client.idle_done()
        print("\nIDLE mode done")
        client.logout()

What I Tried:

  • Adjusted the IDLE_TIMEOUT value to be as short as 2 seconds and as long as 15 minutes, but the issue persists.
  • When I do not renew the idle command, it works fine, but based on my research, the IDLE command will automatically close after 30 minutes of inactivity, and it doesn't handle network exceptions well.

Environment
IMAPClient version: ^3.0.1
Python version: 3.11.4
OS: Windows 11
IMAP Server: imap.gmail.com

Is there a better way to handle the IDLE command to ensure it's always active, doesn't miss any notifications, and properly handles network exceptions?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions