Fork me on GitHub

ruby-gmail by dcparker

A Rubyesque interface to Gmail. Connect to Gmail via IMAP and manipulate emails and labels. Send email with your Gmail account via SMTP. Includes full support for parsing and generating MIME messages.

Example Code:

1) Require gmail

require 'gmail'

2) Start an authenticated gmail session

#    If you pass a block, the session will be passed into the block,
#    and the session will be logged out after the block is executed.
gmail =, password)
# things...
gmail.logout, password) do |gmail|
  # things...

3) Count and gather emails!

# Get counts for messages in the inbox

# Count with some criteria
gmail.inbox.count(:after => Date.parse("2010-02-20"), :before => Date.parse("2010-03-20"))
gmail.inbox.count(:on => Date.parse("2010-04-15"))
gmail.inbox.count(:from => "")
gmail.inbox.count(:to => "")

# Combine flags and options
gmail.inbox.count(:unread, :from => "")

# Labels work the same way as inbox

# Getting messages works the same way as counting: optional flag, and optional arguments
# Remember that every message in a conversation/thread will come as a separate message.
gmail.inbox.emails(:unread, :before => Date.parse("2010-04-20"), :from => "")

4) Work with emails!

# any news older than 4-20, mark as read and archive it...
gmail.inbox.emails(:before => Date.parse("2010-04-20"), :from => "").each do |email|
  email.mark(:read) # can also mark :unread or :spam

# delete emails from X...
gmail.inbox.emails(:from => "x-fiancé").each do |email|

# Save all attachments in the "Faxes" label to a folder
folder = "/where/ever"
gmail.mailbox("Faxes").emails.each do |email|
  if !email.message.attachments.empty?

# Save just the first attachment from the newest unread email (assuming pdf)
# For #save_to_file:
#   + provide a path - save to attachment filename in path
#   + provide a filename - save to file specified
#   + provide no arguments - save to attachment filename in current directory
email = gmail.inbox.emails(:unread).first

# Add a label to a message

# Or "move" the message to a label

5) Create new emails!

Creating emails now uses the amazing Mail rubygem. See its documentation here. Ruby-gmail will automatically configure your Mail emails to be sent via your Gmail account's SMTP, so they will be in your Gmail's "Sent" folder. Also, no need to specify the "From" email either, because ruby-gmail will set it for you.

gmail.deliver do
  to ""
  subject "Having fun in Puerto Rico!"
  text_part do
    body "Text of plaintext message."
  html_part do
    body "<p>Text of <em>html</em> message.</p>"
  add_file "/path/to/some_image.jpg"
# Or, generate the message first and send it later
email = gmail.generate_message do
  to ""
  subject "Having fun in Puerto Rico!"
  body "Spent the day on the road..."
# Or...


shared-mime-info >= 0

tmail >= 1.2.3


sudo gem install ruby-gmail


(The MIT License) Copyright (c) 2009 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Daniel Parker (

Extra thanks for specific feature contributions from:


You can download this project in either zip or tar formats.

You can also clone the project with Git by running:

git clone git://