Skip to content

Instantly share code, notes, and snippets.

@ferblape
Created January 16, 2020 17:21
Show Gist options
  • Save ferblape/939ccfd333584b795767fba45c709a37 to your computer and use it in GitHub Desktop.
Save ferblape/939ccfd333584b795767fba45c709a37 to your computer and use it in GitHub Desktop.
IMAP Ruby idle sample
require "net/imap"
email = "xxxxx@foo.com"
password = "xxxxxx"
server = "imap.xxxxx.com"
imap = Net::IMAP.new(server, 993, true)
imap.login(email, password)
def process_emails
puts "Processing emails...."
end
loop do
begin
imap.select("INBOX")
imap.idle do |resp|
# You'll get all the things from the server. For new emails you're only
# interested in EXISTS ones
if resp.kind_of?(Net::IMAP::UntaggedResponse) and resp.name == "EXISTS"
puts resp
# Got something. Send DONE. This breaks you out of the blocking call
imap.idle_done
end
end
# We're out, which means there are some emails ready for us.
# Go do a seach for UNSEEN and fetch them.
process_emails()
rescue Net::IMAP::Error => e
puts "Something happened: #{e.messsage}"
# Socket probably timed out
rescue Exception => e
puts "Something went terribly wrong: #{e.messsage}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment