Skip to content

Instantly share code, notes, and snippets.

@atonse
Created August 23, 2011 16:47
Show Gist options
  • Save atonse/1165820 to your computer and use it in GitHub Desktop.
Save atonse/1165820 to your computer and use it in GitHub Desktop.
Gmail Poll IMAP
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
@atonse
Copy link
Author

atonse commented Aug 24, 2011

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.

@rwilcox
Copy link

rwilcox commented Aug 24, 2011

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