Skip to content

Instantly share code, notes, and snippets.

@michelson
Created August 6, 2010 22:51
Show Gist options
  • Save michelson/512158 to your computer and use it in GitHub Desktop.
Save michelson/512158 to your computer and use it in GitHub Desktop.
require 'net/pop'
module Mailman
class Receiver
# Receives messages using POP3, and passes them to a {MessageProcessor}.
class POP3
# @return [Net::POP3] the POP3 connection
attr_reader :connection
# @param [Hash] options the receiver options
# @option options [MessageProcessor] :processor the processor to pass new
# messages to
# @option options [String] :server the server to connect to
# @option options [Integer] :port the port to connect to
# @option options [String] :username the username to authenticate with
# @option options [String] :password the password to authenticate with
def initialize(options)
@processor = options[:processor]
@username = options[:username]
@password = options[:password]
@connection = Net::POP3.new(options[:server], options[:port])
@ssl = options[:ssl] ||= false
@connection = Net::POP3.new(options[:server], options[:port])
@connection.enable_ssl(OpenSSL::SSL::VERIFY_NONE) if @ssl
end
# Connects to the POP3 server.
def connect
@connection.start(@username, @password)
end
# Disconnects from the POP3 server.
def disconnect
@connection.finish
end
# Iterates through new messages, passing them to the processor, and
# deleting them.
def get_messages
@connection.each_mail do |message|
@processor.process(message.pop)
message.delete
end
@connection.delete_all
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment