Created
July 7, 2014 21:16
-
-
Save Blackbaud-TitusFortner/bb52768c274a5b3f3e12 to your computer and use it in GitHub Desktop.
WatirmarkEmail::BaseController
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
| def get_email_text(search_array, timeout=600, delete=true, since_sec=3600) | |
| # Trying super ugly workaraound for the gmail 'Too many simlutaneous connections' error. | |
| # Just trying to login to gmail and if it fails try to wait until other connections | |
| # are closed and try again. | |
| email_text = nil | |
| email_subject = nil | |
| email_uid = nil | |
| ::Timeout.timeout(timeout) do | |
| @log.debug("start Timeout block for #{timeout} seconds") | |
| loop do | |
| begin | |
| imap = connect | |
| msgs = imap.search(search_array) | |
| if (msgs && msgs.length > 0) | |
| email_id = msgs.last | |
| email_uid = imap.fetch(email_id, 'UID').last.attr['UID'] | |
| email_text = imap.uid_fetch(email_uid, 'BODY[TEXT]').last.attr['BODY[TEXT]'] | |
| envelope = imap.uid_fetch(email_uid, 'ENVELOPE').last.attr['ENVELOPE'] | |
| email_subject = envelope.subject | |
| end | |
| rescue => e | |
| @log.info("Error connecting to IMAP: #{e.message}") | |
| ensure | |
| if (delete && email_uid) | |
| @log.info("Deleting the email message #{email_subject}") | |
| delete(email_uid, imap) | |
| end | |
| disconnect(imap) unless imap.nil? # because sometimes the timeout happens before imap is defined | |
| end | |
| break if email_text | |
| @log.debug("Couldn't find email yet ... trying again") | |
| sleep 10 | |
| end | |
| end | |
| email_text | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment