Gmail Driven Web Service with Python

I had a basic task: upload video files to a content server. The videos are received by email/gmail and I have to do a lot of manual work to put them up. So I looked to automate this task with my friend Python.

After some research I found libgmail, but the current version is out of date and no longer works with gmail. You will get an error: libgmail.GmailLoginFailure: ‘Login failed. (Wrong username/password?)’). I found a patch for here. Update: this links seems to be broken, but Google still has it here.

Sample code follows:

#!/usr/bin/env python
import libgmail
import os
ga = libgmail.GmailAccount("USERNAME", "PASSWORD")
folder = ga.getMessagesByLabel("video")
for thread in folder:
print "Processing thread:",, "Subject:", thread.subject
for msg in thread:
print "Message:",
for file in msg.attachments:
print "Saving file: " + file.filename
out = open(file.filename, "w")
print "Uploading the file: " + file.filename
os.system("scp " + file.filename + " user@server:")
print "Sending a response to: ", msg.sender
response=libgmail.GmailComposedMessage(msg.sender, thread.subject, "Done")
print "Archiving message: " +

The brilliant thing this example demonstrates is that you can drive any delayed server-side functionality with simple gmail labels. Set this up as a cron job and you have yourself an informal ops web service in less than 30 minutes.  The simplicity of Python allows for a concise and readable code.

2 Responses to Gmail Driven Web Service with Python

  1. jake says:

    Thanks for posting a link to the patch. I was scratching my head for a while trying to get this to work out of the box.

  2. arturnt says:

    You welcome!

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: