Previous Section  < Day Day Up >  Next Section

Hack 80. Program Gmail

Try your hand at writing an alternative interface to Gmail using the freely available Python, Perl, PHP, Java, and .NET libraries and API frameworks.

The relatively simple and lightweight data interface to Gmail stems from the separation between user interface (client-side JavaScript) and data model. This has spawned myriad frontends (graphical and otherwise), libraries, and unofficial "API" implementations in Python, Perl, PHP, Java, and .NET.

For a glimpse of the Gmail engine and protocol underlying the official Gmail interface and the lion's share of the unofficial APIs and libraries written to the service, take a gander at Johnvey Hwang's "About the Gmail engine and protocol" (; scroll down).

Programmatic access to Gmail is accomplished by screen-scraping either the web interface or its underlying data format. While the data format is pretty simple and isn't expected to change dramatically, there's no telling what Google might do that could adversely affect the various programmatic interfaces to their service. Thus, it goes without saying that such hackery comes with no quality of service guarantee. In other words, expect breakages. And if you do notice something's gone wrong, visit the home page of your chosen programmatic interface for the latest version of the code, news, and further information.

Rather than taking you step by step through the same code in each of the five languages and frameworks, I provide a walk through in Python. The APIs are all rather similar, which shouldn't come as any great surprise since they are all built upon the Gmail "API" used by the candy-coated JavaScript-powered Gmail Web interface.

6.13.1. Python

The libgmail (; GNU Public License 2.0/PSF) Python binding for Gmail provides a nice, clean interface (as you'd expect from Python) to your Gmail account.

Libgmail bundles a lovely set of useful example applications, usable right out of the box:

Downloads your Gmail messages to your computer for archiving, importing, or moving purposes.

Proxies SMTP requests, allowing you to use Gmail to send email from the comfort of your preferred email application; a related script,, sends a single email message via Gmail from the command line—not unlike using the Unix mail command.

Proxies a standard POP interface to mail from your preferred email application.

Pretends to be an FTP server, allowing you to download (only) messages labeled "ftp" via a standard FTP application. Installing the hack

Installation is just a matter of downloading and unpacking the library (, or click the Downloads link on the libgmail home page) and putting it someplace findable by Python. The code

Libgmail sports much functionality, each with its own rather self-explanatory function name: getMessagesByQuery, getQuotaInfo, getLabelNames, getMessagesByLabel, getrawMessage, and getUnreadMsgCount. Leaf through for the kind of details only a programmer could love.

Here's a snippet of sample code showing off login, folder selection, and strolling through email—thread by thread, message by message:



# A simple example of the libgmail Python binding for Gmail in action



# Usage: python

import libgmail

# Login

gmail = libgmail.GmailAccount('', '12bucklemyshoe')

gmail.login( )

# Select a folder, label, or starred messages--in this case, the Inbox

folder = gmail.getMessagesByFolder('inbox')

# Stroll through threads in the Inbox

for thread in folder:

  print, len(thread), thread.subject

  # Stroll through messages in each thread

  for msg in thread:

    print "  ",, msg.number, msg.subject

Replace and 12bucklemyshoe with your Gmail email address and password. Instead of inbox, you can use any of Gmail's standard folder names or your custom labels—e.g., starred, sent, or friends.

Save the code to a file called Running the hack

Run the script on the command line, like so:

$ python

WARNING:root:Live Javascript and constants file versions differ.

ff602fe48d89bc3 1 Hello from Hotmail

   ff602fe48d89bc3 1 Hello from Hotmail

ff5fb9c2829c165 1 Hello Gmail via Gmail Loader

   ff5fb9c2829c165 1 Hello Gmail via Gmail Loader

ff5691f7170cb62 1 Hello Gmail via Gmail Loader

   ff5691f7170cb62 1 Hello Gmail via Gmail Loader

ff3f4310237b607 1 Howdy gmail-lite

   ff3f4310237b607 1 Howdy gmail-lite Hacking the hack

Swap in a call to getMessagesByQuery and you now have a command line right to the Gmail search engine:

#folder = gmail.getMessagesByFolder('inbox')

folder = gmail.getMessagesByQuery('from:rael subject:Howdy')

Here are the results of this little switch:

$ python

WARNING:root:Live Javascript and constants file versions differ.

ff3f4310237b607 1 Howdy gmail-lite

   ff3f4310237b607 1 Howdy gmail-lite See also ( is a simple Python interface to Gmail, focusing on exporting raw messages for backup and import.

6.13.2. Perl

Mail::Webmail::Gmail ( provides Perl hackers a programmatic interface to Gmail. You'll find full POD documentation and more sample code than you can shake a stick it in the module and online at the aforementioned URL.

A Comprehensive Perl Archive Network (CPAN) search for gmail ( at the time of this writing finds three more Perl Gmail libraries: Mail::Webmail::Gmail, WWW::GMail, and WWW::Scraper::Gmail.

6.13.3. PHP

GMailer or libgmailer (; GNU Public License) is a PHP library for interacting with Gmail by way of the curl library ( with SSL support ( It is the engine underlying gmail-lite Hack #76, an HTML-only interface to Gmail.

For full libgmailer documentation and plenty of sample code, leaf through the online documentation (

6.13.4. Java

G4j or GMail API for Java (; GNU Public License) is a Java interface to Gmail. The API comes with GMailer for Java, a basic GUI frontend to Gmail built on top of G4j.

Full documentation in Javadoc HTML is available online (

6.13.5. .NET

The Gmail Agent API (; GNU Public License) is a .NET foundation for programming to Gmail. A full package of source for the API itself; the Gmail Agent Applet, a proof of concept Windows frontend to Gmail; and associated Windows Installer projects are available for download.

There's also full documentation available in both HTML format ( and as Windows Help.

    Previous Section  < Day Day Up >  Next Section