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
@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