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.

Installing

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'

Gmail.new(username, password) do |gmail|
  # send commands
end
# logged out

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

require 'gmail'

gmail = Gmail.new(username, password)
# send commands
gmail.logout
# 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 => "phb@work.com") # an array of emails from PHB
inbox.count # how many emails in the mailbox
inbox.count(:unread) # how many unread
inbox.count(:from => "phb@work.com") # how many from PHB

You can also perform many tasks on an individual email:

email = inbox.emails.first

email.mark(:read)
email.archive!
email.delete!
email.label('urgent')
email.move_to('followup')

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
  to "phb@work.com"
  subject "Not feeling well"
  text_part do
    body "I won't be coming in today."
  end
  html_part do
    body "<p>I <em>won't</em> be coming in today.</p>"
  end
end

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

email.deliver!

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.

Advertisements

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