require'net/pop'

Net::POP3

This library provides functionality for retrieving email via POP3, the Post Office Protocol version 3. For details of POP3, see RFC1939.

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.

require'net/pop'pop=Net::POP3.new('pop.example.com')pop.start('YourAccount','YourPassword')# (1)ifpop.mails.empty?puts'No mail.'elsei=0pop.each_maildo|m|# or "pop.mails.each ..." # (2)File.open("inbox/#{i}",'w')do|f|f.writem.popendm.deletei+=1endputs"#{pop.mails.size} mails popped."endpop.finish# (3)
  1. Call Net::POP3#start and start POP session.
  2. Access messages by using POP3#each_mail and/or POP3#mails.
  3. Close POP session by calling POP3#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 Net::POP3.start can be used instead of POP3.new, POP3#start and POP3#finish.

require'net/pop'Net::POP3.start('pop.example.com',110,'YourAccount','YourPassword')do|pop|ifpop.mails.empty?puts'No mail.'elsei=0pop.each_maildo|m|# or "pop.mails.each ..."File.open("inbox/#{i}",'w')do|f|f.writem.popendm.deletei+=1endputs"#{pop.mails.size} mails popped."endend

POP3#delete_all is an alternative for #each_mail and #delete.

require'net/pop'Net::POP3.start('pop.example.com',110,'YourAccount','YourPassword')do|pop|ifpop.mails.empty?puts'No mail.'elsei=1pop.delete_alldo|m|File.open("inbox/#{i}",'w')do|f|f.writem.popendi+=1endendend

And here is an even shorter example.

require'net/pop'i=0Net::POP3.delete_all('pop.example.com',110,'YourAccount','YourPassword')do|m|File.open("inbox/#{i}",'w')do|f|f.writem.popendi+=1end

Memory Space Issues

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

require'net/pop'i=1Net::POP3.delete_all('pop.example.com',110,'YourAccount','YourPassword')do|m|File.open("inbox/#{i}",'w')do|f|m.popdo|chunk|# get a message little by little.f.writechunkendi+=1endend

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:

require'net/pop'# Use APOP authentication if $isapop == truepop=Net::POP3.APOP($isapop).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.

defneed_pop?(id)# determine if we need pop this mail...endNet::POP3.start('pop.example.com',110,'Your account','Your password')do|pop|pop.mails.select{|m|need_pop?(m.unique_id)}.eachdo|m|do_something(m.pop)endend

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

Net::POP3 Reference

Net::APOP Reference

close