Class: Mailman::Receiver::POP3

Inherits:
Object
  • Object
show all
Defined in:
lib/mailman/receiver/pop3.rb

Overview

Receives messages using POP3, and passes them to a MessageProcessor.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ POP3

Returns a new instance of POP3.

Parameters:

  • options (Hash)

    the receiver options

Options Hash (options):

  • :processor (MessageProcessor)

    the processor to pass new messages to

  • :server (String)

    the server to connect to

  • :port (Integer)

    the port to connect to (default 110, or 995 for ssl)

  • :username (String)

    the username to authenticate with

  • :password (String)

    the password to authenticate with

  • :ssl (true, false, Hash)

    enable SSL



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mailman/receiver/pop3.rb', line 19

def initialize(options)
  @processor = options[:processor]
  @username = options[:username]
  @password = options[:password]
  port = options[:port] || (options[:ssl] ? 995 : 110)
  @connection = Net::POP3.new(options[:server], port)
  if options[:ssl].is_a? Hash
    @connection.enable_ssl(options[:ssl])
  elsif options[:ssl]
    @connection.enable_ssl
  end
  @connection.open_timeout = options[:open_timeout] if options[:open_timeout]
  @connection.read_timeout = options[:read_timeout] if options[:read_timeout]
end

Instance Attribute Details

#connectionNet::POP3 (readonly)

Returns the POP3 connection.

Returns:

  • (Net::POP3)

    the POP3 connection



9
10
11
# File 'lib/mailman/receiver/pop3.rb', line 9

def connection
  @connection
end

Instance Method Details

#connectObject

Connects to the POP3 server.



35
36
37
# File 'lib/mailman/receiver/pop3.rb', line 35

def connect
  @connection.start(@username, @password)
end

#disconnectObject

Disconnects from the POP3 server.



40
41
42
# File 'lib/mailman/receiver/pop3.rb', line 40

def disconnect
  @connection.finish
end

#get_messagesObject

Iterates through new messages, passing them to the processor, and deleting them.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mailman/receiver/pop3.rb', line 46

def get_messages
  @connection.each_mail do |message|
    begin
      @processor.process(message.pop)
    rescue StandardError => error
      Mailman.logger.error "Error encountered processing message: #{message.inspect}\n #{error.class.to_s}: #{error.message}\n #{error.backtrace.join("\n")}"
      next
    end
  end
  @connection.delete_all
end

#started?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/mailman/receiver/pop3.rb', line 58

def started?
  @connection.started?
end