Skip to content

Instantly share code, notes, and snippets.

@karlosmid
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karlosmid/f17bc130a9dce8edfe6a to your computer and use it in GitHub Desktop.
Save karlosmid/f17bc130a9dce8edfe6a to your computer and use it in GitHub Desktop.
Check email using pop3 protocol.
require 'net/pop'
include Helpers
class EmailPop
attr_accessor :pop
def initialize
end
def create
self.pop = Net::POP3.new app['pop3.server'], app['pop3.port']
self.pop.enable_ssl
self.pop.start(app['pop3.username'], app['pop3.password'])
end
def destroy
self.pop.finish
self.pop = nil
end
def get_body_of_latest_email
if self.pop.mails.empty?
return nil
else
self.pop.each_mail do |email|
message = email.pop
email.delete
return message
end
end
end
def check_mail
check_mail_twenty_times = 0
email_body = nil
while check_mail_twenty_times <=20 do
check_mail_twenty_times = check_mail_twenty_times + 1
self.create
email_body = get_body_of_latest_email
self.destroy
if email_body != nil
return email_body
end
sleep(0.5)
end
return email_body
end
def mark_all_as_read
self.create
if not self.pop.mails.empty?
self.pop.each_mail do |email|
email.pop
end
end
self.destroy
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment