How do I enable push-notification for IMAP (Gmail) using Python imaplib?

Is there a way to monitor a gmail account using imaplib without polling gmail each time I want to see if there is new mail. Or in other words, I just want the script to be notified of a new message so I can process it right away instead of any lag time between polls.

I see that the IMAP protocol supports this with the IDLE command, but I can't see anything documented with it in the imaplib docs, so any help with this would be great!

Asked by: Grace668 | Posted: 05-10-2021

Answer 1

There isn't something in imaplib that does this, AFAIK (disclamer: I know very little about Python), however, it seems that someone has implemented an IDLE extension for Python which has the same interface as imaplib (which you can swap out with no changes to existing code, apparently):

Answered by: Max851 | Posted: 06-11-2021

Answer 2

Check out ProcImap. It's a more abstract framework on top of libimap and libimap2, providing a nice solution to handle IMAP services. Looks like just the stuff you are looking for, and for me as well. I'm right having the same problem with you and just found ProcImap. Gonna try it for myself.

Answered by: Walter265 | Posted: 06-11-2021

Answer 3

This link shows an example of using IMAP IDLE:

It uses the same library linked to in casperOne's answer (imaplib2).

Answered by: Adrian759 | Posted: 06-11-2021

Answer 4

There is simple patch proposed at implementing [RFC 2177 IMAP IDLE] 3 command in a synchronous way ( to wait for more than 1 IMAP server you have to use threads or other means of parallel execution ). It uses stdlib select to wait on socket including timeout. This patch will eventually be added to stdlib, but tests have to be written first. The IDLE command is what you need for gmail IMAP push-notification. Hope, this will help :)

Answered by: Brad503 | Posted: 06-11-2021

Similar questions

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python