Created
August 23, 2011 16:47
-
-
Save atonse/1165820 to your computer and use it in GitHub Desktop.
Gmail Poll IMAP
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
def self.perform | |
imap = Net::IMAP.new('imap.gmail.com', 993, true) | |
imap.login('MAILBOX', 'PASSWORD') | |
loop do | |
imap.select('INBOX') | |
imap.search(['UNSEEN']).each do |message_id| | |
messages = imap.fetch(message_id, ['RFC822']) | |
messages.each do |message| | |
m = TMail::Mail.parse message.attr['RFC822'] | |
m.body =~ /To\:\[BJ\-(\d+)\].*\r\n/ | |
fax_metadata_id = $1.to_i | |
next unless fax_metadata_id | |
fax_metadata = FaxMetadata.find(fax_metadata_id) | |
fax_metadata.was_successful = !(m.subject =~ /^Fax Delivery Successful/).nil? | |
fax_metadata.save! | |
end | |
end | |
sleep 5 | |
end | |
imap.logout | |
# imap.disconnect | |
end |
I'm not using IMAP IDLE - I'm just polling the inbox every 5 seconds (for now), I just wanted to get rid of the SSL overhead of logging in each time we want to check, and so far in testing, it's been working pretty well .... so far. I saw your comment and looked up IMAP IDLE but it seems like unnecessary added complexity for a mailbox that has way too little traffic. IMAP IDLE has more to do with "push", something we don't need, when polling every few seconds serves the same purpose.
If you dig through the git logs you should see where I try to implement IMAP IDLE... but then fail because of the mentioned deadlocks and/or concurrency issues. But there might be sample code hidden in the history.
Regardless, it is kind of interesting. :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I assume you found my comments about why IMAP IDLE looks like a great way to do this, but it ends up sucking? ;) (For more fun background, search for "IMAP IDLE" in the codebase from which you pulled this gist ;) )