Extra thanks for specific feature contributions from:
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.
require 'gmail'
# 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
# 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]")
# 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|
if !email..empty?
email.(folder)
end
end
# 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
email.[0].save_to_file("/path/to/location")
# Add a label to a message email.label("Faxes")
# Or "move" the message to a label email.move_to("Faxes")
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
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. do
to "[email protected]"
subject "Having fun in Puerto Rico!"
body "Spent the day on the road..."
end
email.deliver!
# Or... gmail.deliver(email)