1. Products
  2.   Email
  3.   Ruby
  4.   Ruby-Gmail
 
  

Open Source Ruby Email Creation & Processing Library 

Free Ruby API that allows allows Ruby Apps to Access and Manipulate Gmail Accounts via IMAP. Compose, Send, Archive, Move & Delete Email Messages via Ruby Library.

What is Ruby-Gmail Library?

In an age where email still plays a crucial role in communication workflows, software developers often need to interact with Gmail programmatically. Whether it's pulling reports, sending automated notifications, or monitoring inboxes, having the right tools can save hours of effort. Ruby-Gmail is an open-source Ruby gem that provides a simple, Ruby-esque interface to Gmail via the IMAP protocol. Created by developer dcparker, the library abstracts away the complexity of dealing with raw IMAP commands and instead gives you readable, expressive methods to work with Gmail inboxes, emails, labels, attachments, and more. With just a couple of lines of Ruby code, software developers can create and send multipart email messages in plaintext and/or html, with inline images and attachments.

Ruby-Gmail is a hidden gem for Rubyists who want a minimal, elegant interface to automate Gmail tasks. The library has include various features for working with Gmail emails, such as authenticate to Gmail, read, search, or filter emails, compose and send messages, remove or delete email, archive messages, access email attachments, organize emails using labels and so on. The library's active development and open source nature mean it continues to improve and adapt to changes in Gmail's API. Apart from some great features there are also some limitation such as OAuth2 is not supported yet and relies on IMAP, which may not expose all Gmail-specific features. It’s a perfect fit for scripting tasks or integrating Gmail into lightweight Ruby applications.

Previous Next

Getting Started with Mail

The easiest way to install the Ruby-Gmail library is via RubyGems. Please use the following command for easy installation.

Install Ruby-Gmail via Rubygems

gem install gmail

Email Authentication inside Ruby Apps

The open source Ruby-Gmail library supports multiple authentication methods including OAuth2 and plain username/password. It allows simple login using a Gmail address and password or app-specific password (recommended for accounts with 2FA). Alternatively, if you need to use OAuth or handle login outside of the script, Ruby-Gmail supports block-based connection. Please note that for security, avoid hardcoding passwords; use environment variables or secrets management. The following code examples demonstrates both methods for handling email authentications processing via Ruby API.

How to Perform Basic & OAuth2 Email Authentication inside Ruby Apps?

require 'gmail'

# Basic authentication (less secure)
gmail = Gmail.connect(username, password)

# OAuth2 authentication (recommended)
gmail = Gmail.connect(:oauth2, username, access_token)

Search and Filter Emails via Ruby API

The Ruby-Gmail library has provided complete functionality for easily reading and searching email messages inside Ruby applications. Developers can access the inbox, unread messages, or search by subject, sender, or date with ease. The library provides a robust search interface that mirrors Gmail's own search operators. The following example demonstrates, how software developers can apply various methods for searching email messages inside Ruby apps.

How to Search Email Messages inside Gmail via Ruby API?

# Search for unread emails from a specific sender
emails = gmail.inbox.emails(:unread, from: 'notifications@example.com')

# Search by subject
emails = gmail.inbox.emails(subject: 'Your monthly report')

# Complex searches
emails = gmail.inbox.emails(after: Date.parse('2023-01-01'), 
                           before: Date.parse('2023-01-31'),
                           has: 'attachment')

Email Composition & Sending via Ruby

The open source Ruby-Gmail library has provided complete support for creating and sending email messaging with just couple of lines of Ruby code. The library makes composing and sending emails straightforward allows adding HTML content, files and images attachments, sending bulk emails, apply styles and formatting, and so on. The following example, shows how software developers can add HTML content and attachments to an email messages inside Ruby Apps.

How to Add HTML Content & Attachments to an Email Message via Ruby Apps?

gmail.deliver do
  to "recipient@example.com"
  subject "Hello from Ruby-Gmail"
  text_part do
    body "This is a test email sent using Ruby-Gmail."
  end
end

# add HTML content and attachments
gmail.deliver do
  to "recipient@example.com"
  subject "Invoice Attached"
  text_part do
    body "Please see the attached invoice."
  end
  add_file "/path/to/invoice.pdf"
end

Archive, Delete, & Move Emails via Ruby API

The Ruby-Gmail library is very feature-rich and can handle email lifecycle operations smoothly inside ruby applications. Software developers can easily manage emails by moving, labeling, marking as read/unread, etc. they can also easily mark emails as starred, unread, or even move them to different folders. The following ruby code example demonstrates, how to Mark Email, add labels and move to folder inside Ruby applications.

How to Mark Email, Add Labels and Move to a Specific Folder via Ruby Library?

email = gmail.inbox.emails.first

# Mark as read/unread
email.read!
email.unread!

# Add/remove labels
email.label('Important')
email.remove_label('Important')

# Move to folder
email.move_to('Archive')