bgneal@4: Introducing weighmail bgneal@4: ##################### bgneal@4: bgneal@4: :date: 2012-05-24 19:30 bgneal@4: :tags: Python, Gmail, IMAPClient, weighmail bgneal@4: :slug: introducing-weighmail bgneal@4: :author: Brian Neal bgneal@4: bgneal@4: Recently my wife approached me and told me that Gmail was warning her that she bgneal@4: was using 95% of her (free) quota. This was a bit surprising, but my wife does a bgneal@4: lot of photography, and sends lots of photos through the mail to various people. bgneal@4: So I began helping her trying to find her large messages. It was then that I bgneal@4: learned that Gmail provides no easy way to do this. You can't sort by size. You bgneal@4: can search for specific attachments, for example .psd or .jpg, and that is what bgneal@4: she ended up doing. bgneal@4: bgneal@4: Surely I thought there must be an easier way. I thought that perhaps Gmail might bgneal@4: have an API like their other products. A bit of searching turned up that the bgneal@4: only API to Gmail is IMAP_. I didn't know anything about IMAP, but I do know bgneal@4: some Python. And sure enough, Python has a library for IMAP called imaplib_. bgneal@4: Glancing through imaplib I got the impression it was a very low-level library bgneal@4: and I began to get a bit discouraged. bgneal@4: bgneal@4: I continued doing some searching and I quickly found IMAPClient_, a high-level bgneal@4: and friendlier library for working with IMAP. This looked like it could work bgneal@4: very well for me! bgneal@4: bgneal@4: I started thinking about writing an application to find big messages in a Gmail bgneal@4: account. The most obvious and natural way to flag large messages would be to bgneal@4: slap a Gmail label on them. But could IMAPClient do this? It didn't look like bgneal@4: it. It turns out that labels are part of a set of `custom Gmail IMAP bgneal@4: extensions`_, and IMAPClient didn't support them. Yet. bgneal@4: bgneal@4: I contacted the author of IMAPClient, `Menno Smits`_, and quickly learned he is bgneal@4: a very friendly and encouraging guy. I decided to volunteer a patch, as this bgneal@4: would give me a chance to learn something about IMAP. He readily agreed and I bgneal@4: dove in. bgneal@4: bgneal@4: The short version of the story is I learned a heck of a lot from reading the bgneal@4: source to IMAPClient, and was able to contribute a patch for Gmail label support bgneal@4: and even some tests! bgneal@4: bgneal@4: After the patch was accepted I went to work on my application, which I have bgneal@4: dubbed weighmail_. I was even able to figure out how to put `weighmail up on bgneal@4: PyPI`_ thanks to Menno's example. bgneal@4: bgneal@4: So if you need a program to categorize your Gmail by message size, I hope bgneal@4: weighmail will meet your needs. Please try it out and feel free to send me bgneal@4: feedback and feature requests on the Bitbucket issue tracker. bgneal@4: bgneal@4: I have used it maybe a half-dozen times on my Gmail account now. My Gmail bgneal@4: account is only about 26% full and I have about 26,300 messages in my "All Mail" bgneal@4: folder. Run times for weighmail have varied from six to 15 minutes when adding 3 bgneal@4: label categories for size. I was (and am) kind of worried that Gmail may lock me bgneal@4: out of my account for accessing it too heavily, but knock on wood it hasn't yet. bgneal@4: Perhaps they rate limit the responses and that is why the run times vary so bgneal@4: much. bgneal@4: bgneal@4: In any event, I hope you find it useful. A big thank you to Menno Smits for his bgneal@4: IMAPClient library, his advice, and working with me on the patch. Hooray for bgneal@4: open source! bgneal@4: bgneal@4: .. _IMAP: http://en.wikipedia.org/wiki/Internet_Message_Access_Protocol bgneal@4: .. _imaplib: http://docs.python.org/library/imaplib.html bgneal@4: .. _IMAPClient: http://imapclient.freshfoo.com/ bgneal@4: .. _custom Gmail IMAP extensions: https://developers.google.com/google-apps/gmail/imap_extensions bgneal@4: .. _weighmail: https://bitbucket.org/bgneal/weighmail/ bgneal@4: .. _weighmail up on PyPI: http://pypi.python.org/pypi/weighmail/0.1.0 bgneal@4: .. _Menno Smits: http://freshfoo.com/blog/