Object
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
Get all emails.
Possible options:
order: order of emails returned. Possible values are :asc or :desc. Default value is :asc.
# File lib/mail/network/retriever_methods/pop3.rb, line 79 79: def all(options = {}, &block) 80: options ||= {} 81: options[:count] = :all 82: find(options, &block) 83: end
Delete all emails from a POP3 server
# File lib/mail/network/retriever_methods/pop3.rb, line 122 122: def delete_all 123: start do |pop3| 124: unless pop3.mails.empty? 125: pop3.delete_all 126: pop3.finish 127: end 128: end 129: 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.
# File lib/mail/network/retriever_methods/pop3.rb, line 93 93: def find(options = {}, &block) 94: options = validate_options(options) 95: 96: start do |pop3| 97: mails = pop3.mails 98: mails.sort! { |m1, m2| m2.number <=> m1.number } if options[:what] == :last 99: mails = mails.first(options[:count]) if options[:count].is_a? Integer 100: 101: if options[:what].to_sym == :last && options[:order].to_sym == :desc || 102: options[:what].to_sym == :first && options[:order].to_sym == :asc || 103: mails.reverse! 104: end 105: 106: if block_given? 107: mails.each do |mail| 108: yield Mail.new(mail.pop) 109: end 110: else 111: emails = [] 112: mails.each do |mail| 113: emails << Mail.new(mail.pop) 114: end 115: emails.size == 1 && options[:count] == 1 ? emails.first : emails 116: end 117: 118: end 119: end
Get the oldest received email(s)
Possible options:
count: number of emails to retrieve. The default value is 1. order: order of emails returned. Possible values are :asc or :desc. Default value is :asc.
# File lib/mail/network/retriever_methods/pop3.rb, line 54 54: def first(options = {}, &block) 55: options ||= {} 56: options[:what] = :first 57: options[:count] ||= 1 58: find(options, &block) 59: end
Get the most recent received email(s)
Possible options:
count: number of emails to retrieve. The default value is 1. order: order of emails returned. Possible values are :asc or :desc. Default value is :asc.
# File lib/mail/network/retriever_methods/pop3.rb, line 67 67: def last(options = {}, &block) 68: options ||= {} 69: options[:what] = :last 70: options[:count] ||= 1 71: find(options, &block) 72: end
Start a POP3 session and ensures that it will be closed in any case.
# File lib/mail/network/retriever_methods/pop3.rb, line 143 143: def start(config = Mail::Configuration.instance, &block) 144: raise ArgumentError.new("Mail::Retrievable#pop3_start takes a block") unless block_given? 145: 146: pop3 = Net::POP3.new(settings[:address], settings[:port], isapop = false) 147: pop3.enable_ssl(verify = OpenSSL::SSL::VERIFY_NONE) if settings[:enable_ssl] 148: pop3.start(settings[:user_name], settings[:password]) 149: 150: yield pop3 151: ensure 152: if defined?(pop3) && pop3 && pop3.started? 153: pop3.reset # This clears all "deleted" marks from messages. 154: pop3.finish 155: end 156: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.