Processing GMail items with Ruby

I had a number of items in my GMail inbox that hadn’t been read.  These were not likely to be read, but I wanted to keep them for posterity.

I pondered putting together some kind of an automated solution to mark all of these items as read, but didn’t give it much thought.

This morning, I happened upon a handy gem for GMail.

http://dcparker.github.com/ruby-gmail/

I installed the gem using the following command:


gem install ruby-gmail

The only problem I had seemed to be a common one.  When attempting to run my first trial script, the require for the gmail gem failed. Based on sage advice from a message-board, I edited gmail/message.rb, commenting out require ‘mime/message’.  This fixed the problem.

The script I built is as follows:


# Copyright ©2012 by James K. Lawless
# See full MIT / X11 license at
# http://www.mailsend-online.com/license2012.php

# Mark all emails in my Gmail inbox as read.

require 'gmail'

if ARGV.length < 2
 puts "Syntax:\tallread.rb gmail-id gmail-password"
 exit 1
end

gmail=Gmail.new(ARGV[0],ARGV[1])
count = 0
gmail.inbox.emails(:unread).each do |msg|
 msg.mark(:read)
 count = count + 1
 puts count
end

To run the script, enter the following command, supplying your GMail credentials:


allread.rb gmail-id gmail-password

After running the script, I closed and reopened my GMail tab to refresh the “unread” stats … which are now non-existent. 😉

Advertisements

About Jim Lawless

I've been programming computers for about 36 years ... 30 of that professionally. I've been a teacher, I've worked as a consultant, and have written articles here and there for publications like Dr. Dobbs Journal, The C/C++ Users Journal, Nuts and Volts, and others.
This entry was posted in Technology and tagged , . Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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