xbiff 2.0: use Python to check your Gmail inbox via IMAP and notify

I confess my ignorance. Only yesterday I found out about IMAP, and how you can use it to access your mailboxes remotely. It is a fairly new protocol (compared to e.g. POP)… And the good thing is both Gmail and Mutt support it. That means it is possible to check your Gmail boxes via mutt without having to download stuff with e.g. fetchmail/procmail.

Don’t get me wrong, I do like fetchmail and procmail. But I guess using mutt with IMAP can certainly be useful sometimes. It’s the best choice to read your mail in someone else’s machine, for example.

Since I felt so dumb to take so long to learn about all this, I decided to rush and start using it, and beyond… I made a small Python script to access my Gmail mailbox via IMAP and notify me about the state of the inbox (i.e. if there are any new messages). The notification happens via modern-day window manager notification tools. I use the Awesome window manager myself, and there is something called Naughty that is part of it and handles these notifications.

So there you have it, “xbiff 2.0”. xbiff monitored spool files and was a old-timer x program. My script does pretty much the same thing, but checking a remote mailbox with IMAP and using a more modern graphical back-end. And all that from Python. Enjoy the code!

Please notice that the code stores sensitive information: your gmail username and password!… You should take care to leave the file non-readable. The whole code is crap, BTW, it can be heavily modified. Have fun! 🙂

#!/usr/bin/env python
import pygtk
import pynotify
import sys

import imaplib, time

if __name__ == '__main__':
    if not pynotify.init("Basics"):

    n = pynotify.Notification("Gmail", "Your Gmail inbox will be checked every minute.")

    if not n.show():
        print "Failed to send notification"

    while True:
        M = imaplib.IMAP4_SSL('imap.gmail.com')
        newz = M.status('INBOX', '(RECENT UNSEEN MESSAGES)')[1][0]
        print '%%%'
        print newz

        if newz == lastz and False:  # remove "and False" to get state changes strictly
            n = pynotify.Notification("Gmail", newz)

            if not n.show():
                print "Failed to send notification"


2 thoughts on “xbiff 2.0: use Python to check your Gmail inbox via IMAP and notify

  1. Elaine

    Thanks, this is exactly what I was looking for, and it
    works perfectly for me — using fvwm with Debian Squeeze

    1. nlw0 Post author

      Great! I’m always happy to help anyone. Not to say getting feedback that my code actually works for someone else. 🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s