Created
April 23, 2011 15:53
-
-
Save duckinator/938722 to your computer and use it in GitHub Desktop.
script to fetch emails from gmail, without marking them as read
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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