Like many people, for a long time I have been drowning in email. I am perhaps bit unusual in that whilst my personal google inboxes are permanently overflowing, my work inboxes are generally very close to empty. This is because as a (bad) practitioner of GTD and sometime reader of Inbox Zero and similar sites, I do actually know how to get grips with email, and through professional pride apply the techniques fairly religiously when I’m working. In contrast, when it comes to dealing with personal mail, I’ll always favour procrastinating on some other interesting project instead. God forbid I should ever get my personal life in gear!
Well, this bad habit has been stressing me out for a LONG time now. I’m a long-term fan of Gretchen Rubin’s Happiness Project, and the other day stumbled across one of her older posts entitled Measure what you want to manage. I’d been wanting to graph the size of my inboxes over time, to get some grip on how bad my backlog actually is, and her post gave me the nudge to actually sort it out.
As usual, Ruby makes it almost ridiculously easy. There’s a beautiful written gem called gmail which uses IMAP to talk to your gmail account. So then it’s just a matter of writing a little program to append a line of gmail folder sizes to a CSV file every time it gets run:
#!/usr/bin/env ruby require 'pathname' require 'rubygems' # sudo gem install gmail # Note: this is an improved version of Daniel Parker's ruby-gmail # http://rubydoc.info/gems/gmail/0.3.4/frames require 'gmail' # sudo gem install fastercsv require 'faster_csv' USERNAME = 'firstname.lastname@example.org' PASSWORD = 'your.password.goes.here' CSV_FILE = Pathname(ENV['HOME']).join( &quot;choose&quot;, &quot;a&quot;, &quot;path&quot;, &quot;to&quot;, &quot;gmail-counts.csv&quot; ) labels = FasterCSV.open(CSV_FILE, 'r').shift[2..-1] FasterCSV.open(CSV_FILE, 'a') do |csv| Gmail.connect(USERNAME, PASSWORD) do |gmail| now = Time.now counts = [ now.to_i.to_s, now.strftime(&quot;%Y/%m/%d.%H:%M:%S&quot;) ] labels.each do |label| unread = label.gsub!(/ unread$/, '') folder = gmail.mailbox(label) count = unread ? folder.count(:unread) : folder.count puts &quot;%s: %d (%d unread, %d read)&quot; % \ [ label, count, folder.count(:unread), folder.count(:read) ] counts &lt;&lt; count end csv &lt;&lt; counts end end
gmail-counts.csv in the folder referenced in the script, whose header line contains a list of the labels you want counted (append
unread if you want the unread count rather than the total, and prefix “special” gmail folders with
[Google Mail]/. Here’s an example of the header followed by a single line of folder counts:
epoch,datetime,INBOX,INBOX unread,[Google Mail]/Drafts,music 1290873125,11/27/2010.15:52:05,555,20,12,1596
0 6,12,18 * * * quietrun /path/to/script/count-gmail
UPDATE 5 months later! (15th May 2011): I finally made it!