Type:
Class
Constants:
Revision : %q$Revision$.split[1]

svn revision of this library

What is This Library?

This library provides functionality for retrieving email via POP3, the Post Office Protocol version 3. For details of POP3, see [RFC1939] (www.ietf.org/rfc/rfc1939.txt).

Examples

Retrieving Messages

This example retrieves messages from the server and deletes them on the server.

Messages are written to files named 'inbox/1', 'inbox/2', .… Replace 'pop.example.com' with your POP3 server address, and 'YourAccount' and 'YourPassword' with the appropriate account details.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
require 'net/pop'
 
pop = Net::POP3.new('pop.example.com')
pop.start('YourAccount', 'YourPassword')             # (1)
if pop.mails.empty?
  puts 'No mail.'
else
  i = 0
  pop.each_mail do |m|   # or "pop.mails.each ..."   # (2)
    File.open("inbox/#{i}", 'w') do |f|
      f.write m.pop
    end
    m.delete
    i += 1
  end
  puts "#{pop.mails.size} mails popped."
end
pop.finish                                           # (3)
  1. Call #start and start POP session.

  2. Access messages by using #each_mail and/or #mails.

  3. Close POP session by calling #finish or use the block form of start.

Shortened Code

The example above is very verbose. You can shorten the code by using some utility methods. First, the block form of ::start can be used instead of ::new, #start and #finish.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
require 'net/pop'
 
Net::POP3.start('pop.example.com', 110,
                'YourAccount', 'YourPassword') do |pop|
  if pop.mails.empty?
    puts 'No mail.'
  else
    i = 0
    pop.each_mail do |m|   # or "pop.mails.each ..."
      File.open("inbox/#{i}", 'w') do |f|
        f.write m.pop
      end
      m.delete
      i += 1
    end
    puts "#{pop.mails.size} mails popped."
  end
end

#delete_all is an alternative for each_mail and delete.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
require 'net/pop'
 
Net::POP3.start('pop.example.com', 110,
                'YourAccount', 'YourPassword') do |pop|
  if pop.mails.empty?
    puts 'No mail.'
  else
    i = 1
    pop.delete_all do |m|
      File.open("inbox/#{i}", 'w') do |f|
        f.write m.pop
      end
      i += 1
    end
  end
end

And here is an even shorter example.

1
2
3
4
5
6
7
8
9
10
require 'net/pop'
 
i = 0
Net::POP3.delete_all('pop.example.com', 110,
                     'YourAccount', 'YourPassword') do |m|
  File.open("inbox/#{i}", 'w') do |f|
    f.write m.pop
  end
  i += 1
end

Memory Space Issues

All the examples above get each message as one big string. This example avoids this.

1
2
3
4
5
6
7
8
9
10
11
12
require 'net/pop'
 
i = 1
Net::POP3.delete_all('pop.example.com', 110,
                     'YourAccount', 'YourPassword') do |m|
  File.open("inbox/#{i}", 'w') do |f|
    m.pop do |chunk|    # get a message little by little.
      f.write chunk
    end
    i += 1
  end
end

Using APOP

The net/pop library supports APOP authentication. To use APOP, use the Net::APOP class instead of the Net::POP3 class. You can use the utility method, Net::POP3.APOP(). For example:

1
2
3
4
5
6
7
require 'net/pop'
 
# Use APOP authentication if $isapop == true
pop = Net::POP3.APOP($is_apop).new('apop.example.com', 110)
pop.start(YourAccount', 'YourPassword') do |pop|
  # Rest of the code is the same.
end

Fetch Only Selected Mail Using 'UIDL' POP Command

If your POP server provides UIDL functionality, you can grab only selected mails from the POP server. e.g.

1
2
3
4
5
6
7
8
9
10
def need_pop?( id )
  # determine if we need pop this mail...
end
 
Net::POP3.start('pop.example.com', 110,
                'Your account', 'Your password') do |pop|
  pop.mails.select { |m| need_pop?(m.unique_id) }.each do |m|
    do_something(m.pop)
  end
end

The Net::POPMail#unique_id method returns the unique-id of the message as a String. Normally the unique-id is a hash of the message.

apop?
  • References/Ruby on Rails/Ruby/Classes/Net/Net::POP3

apop?() Instance Public methods Does this instance use

2025-01-10 15:47:30
default_port
  • References/Ruby on Rails/Ruby/Classes/Net/Net::POP3

default_port() Class Public methods returns the port for

2025-01-10 15:47:30
create_ssl_params
  • References/Ruby on Rails/Ruby/Classes/Net/Net::POP3

create_ssl_params(verify_or_params = {}, certs = nil) Class Public methods Constructs

2025-01-10 15:47:30
started?
  • References/Ruby on Rails/Ruby/Classes/Net/Net::POP3

started?() Instance Public methods true if the

2025-01-10 15:47:30
n_bytes
  • References/Ruby on Rails/Ruby/Classes/Net/Net::POP3

n_bytes() Instance Public methods Returns the total size in bytes of all the

2025-01-10 15:47:30
auth_only 2
  • References/Ruby on Rails/Ruby/Classes/Net/Net::POP3

auth_only(account, password) Instance Public methods Starts a pop3 session,

2025-01-10 15:47:30
finish
  • References/Ruby on Rails/Ruby/Classes/Net/Net::POP3

finish() Instance Public methods Finishes a

2025-01-10 15:47:30
certs
  • References/Ruby on Rails/Ruby/Classes/Net/Net::POP3

certs() Class Public methods returns the :ca_file or :ca_path from

2025-01-10 15:47:30
disable_ssl
  • References/Ruby on Rails/Ruby/Classes/Net/Net::POP3

disable_ssl() Class Public methods Disable SSL for all new instances.

2025-01-10 15:47:30
use_ssl?
  • References/Ruby on Rails/Ruby/Classes/Net/Net::POP3

use_ssl?() Class Public methods returns true if

2025-01-10 15:47:30