Created
January 16, 2020 17:21
-
-
Save ferblape/939ccfd333584b795767fba45c709a37 to your computer and use it in GitHub Desktop.
IMAP Ruby idle sample
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" | |
email = "[email protected]" | |
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