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

4

27

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!

Dyna answered 7/1, 2009 at 17:13 Comment(0)
S
16

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):

https://github.com/imaplib2/imaplib2

Snow answered 7/1, 2009 at 18:4 Comment(3)
+1 - I was using imaplib recently and found the state of things that you describe.Minutely
The provided link no longer exists. The package can be found on sourceforgeStrappado
github.com/jazzband/imaplib2Directly
H
6

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.

Hornpipe answered 14/1, 2009 at 6:44 Comment(3)
wow. this is just what I'm looking for to write client-side filters.Jethro
@Hornpipe did this work for you? I'm also looking to get push notifications about new messages instead of polling. What function in ProcImap did you use?Harbard
@Harbard I'd guess ImapServer.idleMammal
R
3

This link shows an example of using IMAP IDLE: http://blog.timstoop.nl/2009/03/11/python-imap-idle-with-imaplib2/

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

Rue answered 28/1, 2011 at 21:23 Comment(1)
It's now at blog.timstoop.nl/posts/2009/03/11/…Irwin
A
2

There is simple patch proposed at bugs.python.org 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 :)

Arnuad answered 19/4, 2013 at 20:26 Comment(1)
Sadly the patch is still not part of the stdlib. One can use imaplib2 hosted on sourceforge or imapclient hosted on githubStrappado

© 2022 - 2024 — McMap. All rights reserved.