Skip to content

Instantly share code, notes, and snippets.

@duckinator
Created April 23, 2011 15:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duckinator/938722 to your computer and use it in GitHub Desktop.
Save duckinator/938722 to your computer and use it in GitHub Desktop.
script to fetch emails from gmail, without marking them as read
require 'net/imap'
require 'highline/import'
# Get username and password from console, via highline
username = ask('Username: ')
password = ask('Password: ') {|q| q.echo = false }
messages = {} # Because messages[7239]=... makes an ugly array
imap = Net::IMAP.new('imap.gmail.com', 993, true) # Connect to server
imap.login(username, password) # Authenticate
imap.select('INBOX') # Open the inbox
imap.search(['NOT', 'SEEN']).each do |message_id|
msg = imap.fetch(message_id, "RFC822")[0] # Read message
messages[msg.seqno] = msg.attr["RFC822"] # Store content locally
imap.store(message_id, "-FLAGS", [:Seen]) # Mark as unread
end
imap.logout() # Log out. Still connected.
imap.disconnect() # Disconnect
# Debug printing stuff
messages.each do |key,val|
puts "#{key}:"
val.split("\n").map { |l| puts " #{l}" }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment