Had a little fun today trying to get IMAP IDLE from Python working. It's not in the default imaplib, but Piers Lauder's imaplib2 has support for it. (I read on another blog that Piers Lauder is also the writer of imaplib, so that's kinda nice.)
I don't really program a lot anymore, so I had some trouble trying to understand how it's supposed to work. Luckily, the blog post that I linked above, also had a link to the git sources of offlineimap, which uses it. Their imapserver.py has a nice implementation that I kind of copied.
As a proof of concept, and hopefully so others can use this too, I created the script below to see if I could get it to work. It doesn't do a whole lot, just print a line when the mailbox gets a new mail. But you can build on it from there. I need these kind of examples to get stuff like this, so I hope it helps someone else! Leave me a comment if it does.
import imaplib2, time
from threading import *
This is the threading object that does all the waiting on
class Idler(object): def init(self, conn): self.thread = Thread(target=self.idle) self.M = conn self.event = Event()def start(self): self.thread.start() def stop(self): # This is a neat trick to make thread end. Took me a # while to figure that one out! self.event.set() def join(self): self.thread.join() def idle(self): # Starting an unending loop here while True: # This is part of the trick to make the loop stop # when the stop() command is given if self.event.isSet(): return self.needsync = False # A callback method that gets called when a new # email arrives. Very basic, but that's good. def callback(args): if not self.event.isSet(): self.needsync = True self.event.set() # Do the actual idle call. This returns immediately, # since it's asynchronous. self.M.idle(callback=callback) # This waits until the event is set. The event is # set by the callback, when the server 'answers' # the idle call and the callback function gets # called. self.event.wait() # Because the function sets the needsync variable, # this helps escape the loop without doing # anything if the stop() is called. Kinda neat # solution. if self.needsync: self.event.clear() self.dosync() # The method that gets called when a new email arrives. # Replace it with something better. def dosync(self): print "Got an event!"
Had to do this stuff in a try-finally, since some testing
went a little wrong.....
try: # Set the following two lines to your creds and server M = imaplib2.IMAP4_SSL("mail.example.com") M.login("mylogin","mypassword") # We need to get out of the AUTH state, so we just select # the INBOX. M.select("INBOX") # Start the Idler thread idler = Idler(M) idler.start() # Because this is just an example, exit after 1 minute. time.sleep(1*60) finally: # Clean up. idler.stop() idler.join() M.close() # This is important! M.logout()