• Stars
    star
    790
  • Rank 57,622 (Top 2 %)
  • Language
    Ruby
  • Created about 15 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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.

Notice

I have pushed verion 0.3.1 to rubygems.org (Yay, thanks Nick Quaranto for the help) which is a build straight from master.

Second, this gem is getting back on track. See this issue here for more information.

ruby-gmail

Author(s)

  • Daniel Parker of BehindLogic.com
  • Nathan Herald

Extra thanks for specific feature contributions from:

Description

A Rubyesque interface to Gmail, with all the tools you'll need. Search, read and send multipart emails; archive, mark as read/unread, delete emails; and manage labels.

Features

  • Search emails
  • Read emails (handles attachments)
  • Emails: Label, archive, delete, mark as read/unread/spam
  • Create and delete labels
  • Create and send multipart email messages in plaintext and/or html, with inline images and attachments
  • Utilizes Gmail's IMAP & SMTP, MIME-type detection and parses and generates MIME properly.

Problems:

  • May not correctly read malformed MIME messages. This could possibly be corrected by having IMAP parse the MIME structure.
  • Cannot grab the plain or html message without also grabbing attachments. It might be nice to lazy-[down]load attachments.

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 = Gmail.new(username, password)
# ...do things...
gmail.logout

Gmail.new(username, password) do |gmail|
  # ...do things...
end

3) Count and gather emails!

# Get counts for messages in the inbox
gmail.inbox.count
gmail.inbox.count(:unread)
gmail.inbox.count(:read)

# 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 => "[email protected]")
gmail.inbox.count(:to => "[email protected]")

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

# Labels work the same way as inbox
gmail.mailbox('Urgent').count

# 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 => "[email protected]")

# Search the same way you do on Gmail's web interface:
gmail.inbox.emails(gm: 'before:2010-02-20 after:2010-03-20')
gmail.inbox.emails(gm: 'on:2010-04-15')
gmail.inbox.emails(gm: 'from:[email protected]')
gmail.inbox.emails(gm: 'to:[email protected]')
gmail.inbox.emails(gm: 'is:read from:[email protected] "you are a great employee"')

# Get messages without marking them as read on the server.
gmail.peek = true
gmail.inbox.emails(:unread, :before => Date.parse("2010-04-20"), :from => "[email protected]")

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 => "[email protected]").each do |email|
  email.mark(:read) # can also mark :unread or :spam
  email.archive!
end

# delete emails from X...
gmail.inbox.emails(:from => "x-fiancΓ©@gmail.com").each do |email|
  email.delete!
end

# Save all attachments in the "Faxes" label to a folder
folder = "/where/ever"
gmail.mailbox("Faxes").emails.each do |email|
  email.attachments.each do |attachment|
    file = File.new(folder + attachment.filename, "w+")
    file << attachment.decoded
    file.close
  end
end

# Add a label to a message
email.label("Faxes")

# Or "move" the message to a label
email.move_to("Faxes")

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 "[email protected]"
  subject "Having fun in Puerto Rico!"
  text_part do
    body "Text of plaintext message."
  end
  html_part do
    content_type 'text/html; charset=UTF-8'
    body "<p>Text of <em>html</em> message.</p>"
  end
  add_file "/path/to/some_image.jpg"
end
# Or, generate the message first and send it later
email = gmail.generate_message do
  to "[email protected]"
  subject "Having fun in Puerto Rico!"
  body "Spent the day on the road..."
end
email.deliver!
# Or...
gmail.deliver(email)

Requirements

  • ruby
  • net/smtp
  • net/imap
  • tmail
  • shared-mime-info rubygem (for MIME-detection when attaching files)

Install

gem install ruby-gmail

License

(The MIT License)

Copyright (c) 2009 BehindLogic

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.

More Repositories

1

jquery_plugins

Just a collection of my jquery plugins.
JavaScript
75
star
2

temporals

"We could develop some interpreter that would be able to parse and process a range of expressions that we might want to deal with. This would be quite flexible, but also pretty hard" (Martin Fowler, http://martinfowler.com/apsupp/recurring.pdf). Temporals is a Ruby parser for just that.
Ruby
64
star
3

quickbooks

A *primitive* Ruby implementation of the QuickBooks API (QBXML). The mature version is for sale at http://behindlogic.com.
Ruby
42
star
4

days_and_times

Natural language method chaining for Time, Durations and the like.
Ruby
32
star
5

MacVault

A git-based set-and-forget backup utility for OSX.
Ruby
20
star
6

shopify

A rubygem for easy interfacing with Shopify.com's restful API.
Ruby
11
star
7

gmailboomerang

GTD - Brings messages back to the inbox that you label Tomorrow, Next Week, and Next Month, when appropriate.
Ruby
9
star
8

osstudybible

OpenSource Study Bible - Online Bible study site created by open-source collaboration!
JavaScript
8
star
9

server_scripts

Scripts to automate management of sites/configs/services on a server using nginx, haproxy, and cilantro.
7
star
10

cilantro

A framework build on top of Sinatra, boasting automagic gem management, auto-reloading in development, and an innovative way to manage templates.
JavaScript
7
star
11

finch

An envelope-based financial tracker to visualize your spending ahead of time. Budgeting in the works.
JavaScript
5
star
12

quickbooks-sdk-docs

A little application for browsing QuickBooks objects and their attributes.
Ruby
5
star
13

httphere

Simply serves a directory's files via HTTP from the command line!
Ruby
4
star
14

musicman

A remote Music Manager for a Ubuntu server to be used as a music box.
JavaScript
3
star
15

involved

An inbox-centric application that aggregates email, tasks, and appointments.
3
star
16

dcparker.github.com

My Github pages.
2
star
17

immortalize

Run a command immortalized to keep reviving it whenever it dies, and notify if failures are too frequent.
Ruby
2
star
18

dcas-ruby

Ruby codebase for managing payments with DCAS.
Ruby
2
star
19

rubygems-vhost

Extensions to rubygems to make it share well for several users on a virtual-host server.
Ruby
2
star
20

cilantro_with_oauth

Just building a little cilantro basic oauth plugin...
JavaScript
2
star
21

simplemapper

A lightweight ORM that gets all its abilities from plugins. A decent ActiveResource replacement. Can handle cross-database associations, complex data types.
Ruby
2
star
22

saferuby

Development repo aiming to create a nice environment for running user-submitted ruby web scripts.
1
star
23

rdoc-inside

Makes rdoc documentation for the currently-loaded code available while inside ruby.
Ruby
1
star
24

ruby-dcop

Auto-generates ruby classes for a DCOP structure. Swicked-Awesome!
Ruby
1
star