Skip to content

Instantly share code, notes, and snippets.

@scottjbarr
Created October 7, 2009 14:50
Show Gist options
  • Save scottjbarr/204104 to your computer and use it in GitHub Desktop.
Save scottjbarr/204104 to your computer and use it in GitHub Desktop.
Search Gmail Inbox
#
# Search gmail via imap
# Author : Scott Barr
#
require 'rubygems'
require 'net/imap'
puts "Authenticating..."
imap = Net::IMAP.new('imap.gmail.com', '993', true)
imap.login('your-gmail-usernanme', 'your-password')
imap.select('INBOX')
#
# imap search examples...
#
# imap.search(["FROM", "ann", "NOT", "NEW", "SINCE", "1-Apr-2003",
# "BODY", "tree"]).each do |msg_id|
# imap.search(["TO", "reggie", "BODY", "vacation"]).each do |msg_id|
#
puts "Searching messages..."
# , "BODY", "account value"
imap.search(['SUBJECT', 'twitter']).each do |msg_id|
msg = imap.fetch(msg_id, "(UID RFC822.SIZE ENVELOPE BODY[TEXT])")[0]
body = msg.attr["BODY[TEXT]"]
# puts "#{body}"
#envelope = msg.attr["ENVELOPE"]
#uid = msg.attr["UID"]
#size = msg.attr["RFC822.SIZE"]
#puts "To: #{to}"
env = imap.fetch(msg_id, "ENVELOPE")[0].attr["ENVELOPE"]
puts "#{env.date} : From #{env.from[0].name} : \t#{env.subject}"
end
imap.logout
imap.disconnect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment