Created
June 24, 2010 12:44
-
-
Save wjessop/451401 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 'rubygems' | |
require 'tmail' | |
class Net::IMAP | |
def idle | |
cmd = "IDLE" | |
synchronize do | |
tag = generate_tag | |
put_string(tag + " " + cmd) | |
put_string(CRLF) | |
end | |
end | |
def done | |
cmd = "DONE" | |
synchronize do | |
put_string(cmd) | |
put_string(CRLF) | |
end | |
end | |
end | |
#Net::IMAP::debug = true | |
imap = Net::IMAP.new('imap.gmail.com', 993, true) | |
imap.login(username, password) | |
imap.select('INBOX') | |
newmail = false | |
imap.add_response_handler { |resp| | |
if resp.kind_of?(Net::IMAP::UntaggedResponse) and resp.name == "EXISTS" | |
newmail = true | |
end | |
} | |
imap.idle | |
puts "Ready" | |
loop do | |
if newmail | |
since = (Date.today - 1).strftime("%d-%b-%Y") | |
puts "Looking for new mail since #{since}" | |
puts "DONE result is #{imap.done}" | |
imap.search("UNSEEN SINCE #{since}").each do |message_id| | |
msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822'] | |
mail = TMail::Mail.parse(msg) | |
puts mail.subject | |
imap.store(message_id, "+FLAGS", [:Seen]) | |
end | |
puts "IDLE result is #{imap.idle}" | |
newmail = false | |
else | |
sleep 0.5 | |
end | |
end | |
#imap.logout() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment