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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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. :)