The Pop3 retriever allows to get the last, first or all emails from a POP3 server. Each email retrieved (RFC2822) is given as an instance of Message.
While being retrieved, emails can be yielded if a block is given.
Mail.defaults do retriever_method :pop3, { :address => "pop.gmail.com", :port => 995, :user_name => '<username>', :password => '<password>', :enable_ssl => true } end Mail.all #=> Returns an array of all emails Mail.first #=> Returns the first unread email Mail.last #=> Returns the first unread email
You can also pass options into Mail.find to locate an email in your pop mailbox with the following options:
what: last or first emails. The default is :first. order: order of emails returned. Possible values are :asc or :desc. Default value is :asc. count: number of emails to retrieve. The default value is 10. A value of 1 returns an instance of Message, not an array of Message instances. Mail.find(:what => :first, :count => 10, :order => :asc) #=> Returns the first 10 emails in ascending order
# File lib/mail/network/retriever_methods/pop3.rb, line 37 37: def initialize(values) 38: self.settings = { :address => "localhost", 39: :port => 110, 40: :user_name => nil, 41: :password => nil, 42: :authentication => nil, 43: :enable_ssl => false }.merge!(values) 44: end
Returns the connection object of the retrievable (IMAP or POP3)
# File lib/mail/network/retriever_methods/pop3.rb, line 102 102: def connection(&block) 103: raise ArgumentError.new('Mail::Retrievable#connection takes a block') unless block_given? 104: 105: start do |pop3| 106: yield pop3 107: end 108: end
Delete all emails from a POP3 server
# File lib/mail/network/retriever_methods/pop3.rb, line 92 92: def delete_all 93: start do |pop3| 94: unless pop3.mails.empty? 95: pop3.delete_all 96: pop3.finish 97: end 98: end 99: end
Find emails in a POP3 mailbox. Without any options, the 5 last received emails are returned.
Possible options:
what: last or first emails. The default is :first. order: order of emails returned. Possible values are :asc or :desc. Default value is :asc. count: number of emails to retrieve. The default value is 10. A value of 1 returns an instance of Message, not an array of Message instances. delete_after_find: flag for whether to delete each retreived email after find. Default is false. Use #find_and_delete if you would like this to default to true.
# File lib/mail/network/retriever_methods/pop3.rb, line 58 58: def find(options = {}, &block) 59: options = validate_options(options) 60: 61: start do |pop3| 62: mails = pop3.mails 63: pop3.reset # Clears all "deleted" marks. This prevents non-explicit/accidental deletions due to server settings. 64: mails.sort! { |m1, m2| m2.number <=> m1.number } if options[:what] == :last 65: mails = mails.first(options[:count]) if options[:count].is_a? Integer 66: 67: if options[:what].to_sym == :last && options[:order].to_sym == :desc || 68: options[:what].to_sym == :first && options[:order].to_sym == :asc || 69: mails.reverse! 70: end 71: 72: if block_given? 73: mails.each do |mail| 74: new_message = Mail.new(mail.pop) 75: new_message.mark_for_delete = true if options[:delete_after_find] 76: yield new_message 77: mail.delete if options[:delete_after_find] && new_message.is_marked_for_delete? # Delete if still marked for delete 78: end 79: else 80: emails = [] 81: mails.each do |mail| 82: emails << Mail.new(mail.pop) 83: mail.delete if options[:delete_after_find] 84: end 85: emails.size == 1 && options[:count] == 1 ? emails.first : emails 86: end 87: 88: end 89: end
Start a POP3 session and ensure that it will be closed in any case. Any messages marked for deletion via # or with the :delete_after_find option will be deleted when the session is closed.
# File lib/mail/network/retriever_methods/pop3.rb, line 125 125: def start(config = Configuration.instance, &block) 126: raise ArgumentError.new("Mail::Retrievable#pop3_start takes a block") unless block_given? 127: 128: pop3 = Net::POP3.new(settings[:address], settings[:port], false) 129: pop3.enable_ssl(OpenSSL::SSL::VERIFY_NONE) if settings[:enable_ssl] 130: pop3.start(settings[:user_name], settings[:password]) 131: 132: yield pop3 133: ensure 134: if defined?(pop3) && pop3 && pop3.started? 135: pop3.finish 136: end 137: end
Set default options
# File lib/mail/network/retriever_methods/pop3.rb, line 113 113: def validate_options(options) 114: options ||= {} 115: options[:count] ||= 10 116: options[:order] ||= :asc 117: options[:what] ||= :first 118: options[:delete_after_find] ||= false 119: options 120: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.