Hacking Gmail - Part 3

I CAN'T HACK GMAIL ACCOUNTS, SO STOP ASKING. Thanks.

I have been busy the last day and I had a stack of Todoist related emails to answer. In Gmail I don't have the option to see all the unanswered emails. But, no problem, it took me about 3 minutes to write a script that does that :) It's amazingly simple to script Gmail, and one could do all kind of analytics.

You'll only need Python and libgmail.

The code

Here is the code that will extract all the unanswered emails according to a query and print a link to them. With some pipe loving you can pipe the output, view it in a browser and answer those emails fast.

import libgmail

ga = libgmail.GmailAccount("<USER>@gmail.com", "<PASSWORD>")
ga.login()
messages = ga.getMessagesByQuery('label:todoist', True)

link = '<a href="http://mail.google.com/mail/\
?fs=1&tf=1&source=atom&view=cv&search=all\
&th=%(id)s&shva=1">%(subject)s</a>'

for thread in messages:
    if len(thread) == 1:
        d = {'id': thread.id, 'subject': thread.subject}
        print '<p>%s</p>' % (link % d)

How to run it

Run this script by doing this:

python answer_todoist_emails.py > emails_to_answer.html

Open emails_to_answer.html in a browser:

Gmail hack 1

Clicking on a link will open up Gmail light (i.e. only the current thread):

Gmail hack 2

If you liked this hack, you may like the others:

7. Mar 2007 Code · Python · Tips
© Amir Salihefendic