Home > Linux, Ubuntu > A desktop notifier for new emails in your Gmail inbox for Ubuntu

A desktop notifier for new emails in your Gmail inbox for Ubuntu

June 11th, 2009

I have been fiddling with the Python programming language over the past few days.  And even though it has just been few weeks and I hardly have covered every aspect of the language in detail, I am already producing working code in python. In short, I am flying! Python really rocks!

So yesterday I thought it would be nice to be able to check my Gmail inbox for new incoming mails from the command line. So a little searching gave me this link.

This is a python script that does exactly what I wanted. But it runs from the command line and is interactive in that it asks for the user name and password from the user interactively. I went a step ahead and thought what if I can make this process automated and for every new mail, show up a notification. Speaking of notifications, you must be aware of the new OSD notification that is creating much buzz these days, especially in Ubuntu circles. This new notification system is designed to replace the existing notification system with a cooler UI and better usability. At least that’s what the canonical folks claim.

Anyway, I set out to create this cool notifier and in a few hours it was ready. Being a first timer to Python, it took some time as every now and then I would stop to see some documentation and then once I understood the concept, carried on hacking. Once complete, the next few minutes I spent in fine tuning the notifier, like add an image (the gmail logo of course!!).

Eureka!! It worked, Yippee !! That’s what I like most about Python. Even a complete newbie like me can create working code in such a short time! Having written the it, now I wanted to make it more user friendly. So the next day, I ended up writing a setup.py script to provide ease of installation. Believe me, writing this setup was far more difficult than the original notification script ! But because of this script, installation is super-easy. Just run the setup.py file, that’s it!

So finally, it was done. I did some minimal testing but it should work well. You may download this from here.

Steps to install:

0. First make sure you have the python bindings for notify-osd, the new notification system for Ubuntu. If not, then install it with the below command:

$ sudo apt-get install python-notify

1. Download, gunzip and untar the gmailnotifier.tar.gz file from the above link.
2. Go to the gmailnotifier folder so created and run the setup.py file.

$ ./setup.py

It will ask a few questions, like your gmail username, password, where you want to install the script and what should be the time interval in seconds to periodically check your mailbox for new mails. Make sure you answer those questions carefully and correctly. I have not added error checking for those questions yet :-|.

But if you feel anything is not correct, you may simply press Ctrl-C, abort the script and then run it from the beginning.

3. Now simply log off from gnome and then log back in. You are all set! That’s it. And don’t worry my setup script makes sure that the configuration file is not world-readable, since your Gmail password is there in plain text.

Enjoy ! In the event that you want to uninstall my script, you can do it manually(since uninstall option is not present in the setup.py script yet). Just open the file ~/.config/autostart/gmailnotifier.desktop in your favorite text editor. Note down the line which has an ‘Exec’ in it as the first word. That line would be something like:

Exec=/path/given/while/running/setup.py/gmailnotifier/gmailnotifier.py

Note down the path, excluding the gmailnotifier part. Here it would be

/path/given/while/running/setup.py

Go to that path and delete the gmailnotifier folder altogether. After that, close the ~/.config/autostart/gmailnotifier.desktop file and delete that also. That’s it.

And in case you are wondering how this looks like, here is a screenshot of it in my Ubuntu Jaunty installation. Look at the top right hand corner. It says I have 3 new mails.

gmailnotifier_in_action

Also, if you have not noticed by now, once installed my notifier script installs itself as a gnome startup program. If you go to ‘System->Preferences->Startup Applications’ you can see a new entry named ‘gmailnotifier’. Here is a screenshot of that too, just so that you don’t get surprised later when you see this new thing there all by itself.

gmailnotifier_installs_itself_as_a_startup_program

Read on if you want to know the technical details of my notifier. Basically my notifier depends on chiefly two main libraries - glib and pynotify. Glib is a general purpose library that is the foundation for GTK+ but more than that, it is a very high quality general purpose library that you can use freely in your other programs. Pynotify, on the other hand gives python bindings to the libnotify library.

In my script I first create a glib main loop. A main loop is an event loop that waits for events waiting indefinitely. Yeah, you guessed it, a glib main loop is at the heart of all GTK+ GUI applications which are fundamentally event driven and respond to mouse clicks, drags/drops and key events. But before I start the loop, I register for a timer callback every n seconds. This n is the interval the script would check your inbox for new mails.

This is one of the values you supply while running the setup script. Anyway, whenever this timer expires, the timer callback is called and this callback logs in, checks the Gmail RSS feed and informs you if there are any new mails. The point to be noted here is that new mails here mean mails that are ‘new’ to the script not to your inbox. Since this script does not mark new mails as read after displaying them, it maintains its own 2 buffers of old mails and new mails respectively. Any mail that is present in the new mail buffer but not in the old mail buffer is flagged as new and this count is what is shown in the the pop up.

As usual, comments/suggestions/bug reports are welcome if you liked my gmailnotifier!

admin Linux, Ubuntu , , ,

  1. Kuldeep
    June 11th, 2009 at 19:47 | #1

    Nice work buddy…Will it work for dumb people like me using Windows or only for geeks like you …

  2. uday kumar
    June 23rd, 2009 at 19:21 | #2

    hai frnd even i am working on the notification project so looking for the help of geeks like u can u help me when i was trying to follow the steps shown above i was unable to install gunzip what is it.

  3. June 23rd, 2009 at 19:41 | #3

    @Uday,
    Sorry about that. The command is actually called ‘gunzip’. It is the opposite of ‘gzip’ - or GNU Zip. It should be installed by default on your system. If not, then assuming you have a debian based distro, give the following command as root to install the gzip utility:

    apt-get install gzip

    Hope this will help.

    ~seemanta

  4. uday kumar
    June 23rd, 2009 at 19:58 | #4

    I’m tryng to execute the gmail notifier prog that is given in the download link but i am getting the following errors can u please help me out.
    Traceback (most recent call last):
    File “gmailnotifier.py”, line 146, in
    read_config_file()
    File “gmailnotifier.py”, line 62, in read_config_file
    config.read(sys.argv[100])
    IndexError: list index out of range.
    Thank u for ur reply……..

  5. uday kumar
    June 23rd, 2009 at 22:00 | #5

    hai friend i am developing a screenlet for gnome desktop and struck up with some problem i.e i was able to display the subject of mail in the inbox to the screenlet and the problem is i want to link to mail evolution when i click on the subject which is in the screenlet.
    so how can i establish that link using python.
    can u plz help me my id is uday409@gmail.com

  6. uday kumar
    June 24th, 2009 at 18:13 | #6

    hai friend i hav a problem in executing the code of u r notifier can u make it executable and send me please i want to see how it execute it’s urgent

  7. September 1st, 2009 at 13:59 | #7

    This site is so fascinating that I decide to post something here!

  8. September 4th, 2009 at 00:51 | #8

    Nice one! I’m a python fan too and a newbie. I recommend hosting your projects at Google Code, github or facilities like them.These are few of my python codes if u like to see:
    http://bit.ly/Hp2vf
    http://bit.ly/18Hxv8

  1. No trackbacks yet.