Emailify Your App with Gmail and Ruby | Fuel Your Coding

Emailify Your App with Gmail and Ruby

Posted May 13th, 2010 / By Jerod Santo in Ruby

Sending and receiving email from your application isn’t hard, but lets be honest, its not as easy as you’d like either. If you haven’t considered using Gmail to manage inbound/outbound email, you really should! The advantages of using Gmail over a local SMTP server or Sendmail include:

  • No tricky mail server configuration
  • Google’s spam filtering is excellent
  • Free archive/backup of your email history
  • Easy manual administration when necessary

Any language with IMAP capabilities in its core, standard library, or 3rd party ecosystem can connect to Gmail for email processing. If that language happens to be Ruby, it’s even easier thanks the the ruby-gmail gem by Daniel Parker. Check it out.


Easy peasy.

gem install ruby-gmail

Connecting to Gmail

Connecting up is quite easy, and there are two options for how to proceed. First, you can perform all your activities inside a block. The advantage of this is the library will automatically log you out at the end of the block:

require 'gmail', password) do |gmail|
  # send commands
# logged out

Second, you can store the connection in a variable and logout it explicitly when done:

require 'gmail'

gmail =, password)
# send commands
# logged out

Managing Email

The mailbox method takes a label as an argument and returns an object which has access to email with the given label.

inbox  = gmail.mailbox('inbox')
urgent = gmail.mailbox('urgent')

Since inbox is such a common label to access, there’s a handy shortcut method:

inbox = gmail.inbox

Now that you have a mailbox object in hand, you can access its emails or get counts:

inbox.emails #  an array of all emails in mailbox
inbox.emails(:unread) # an array of unread emails
inbox.emails(:from => "[email protected]") # an array of emails from PHB
inbox.count # how many emails in the mailbox
inbox.count(:unread) # how many unread
inbox.count(:from => "[email protected]") # how many from PHB

You can also perform many tasks on an individual email:

email = inbox.emails.first


Sending Email

Creating and sending emails is also a breeze. Just like connecting you can use the idiomatic Ruby block method, or the more object-oriented approach:

# with a block
gmail.deliver do
  subject "Not feeling well"
  text_part do
    body "I won't be coming in today."
  html_part do
    body "<p>I <em>won't</em> be coming in today.</p>"

# or generate message and send it later
email = gmail.generate_message do
  subject "Not feeling well"
  body "I won't be coming in today."


The library will take care of sending it out your Gmail account and even set the “From” header for you. Nice!

Try It

If you’re already using Ruby to build an application and you need it to send or receive email, there is little excuse not to let Ruby-Gmail do the heavy lifting for you. Give it a try!

Jerod Santo is an Editor at Fuel Your Coding and a contract software developer at RSDi where he works daily with Ruby, JavaScript, and related technologies. He loves shiny toys, powerful tools, and open-source software. Learn more about Jerod by visiting his homepage or following him on Twitter.

Leave a Reply

Your email address will not be published. Required fields are marked *