Class: Mail::POP3
- Inherits:
-
Object
- Object
- Mail::POP3
- Defined in:
- lib/mail/network/retriever_methods/pop3.rb
Overview
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.
Example of retrieving Emails from GMail:
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
Instance Attribute Summary collapse
-
#settings ⇒ Object
Returns the value of attribute settings.
Instance Method Summary collapse
-
#all(options = {}, &block) ⇒ Object
Get all emails.
-
#find(options = {}, &block) ⇒ Object
Find emails in a POP3 mailbox.
-
#first(options = {}, &block) ⇒ Object
Get the oldest received email(s).
-
#initialize(values) ⇒ POP3
constructor
A new instance of POP3.
-
#last(options = {}, &block) ⇒ Object
Get the most recent received email(s).
Constructor Details
#initialize(values) ⇒ POP3
Returns a new instance of POP3.
37 38 39 40 41 42 43 44 |
# File 'lib/mail/network/retriever_methods/pop3.rb', line 37 def initialize(values) self.settings = { :address => "localhost", :port => 110, :user_name => nil, :password => nil, :authentication => nil, :enable_ssl => false }.merge!(values) end |
Instance Attribute Details
#settings ⇒ Object
Returns the value of attribute settings.
46 47 48 |
# File 'lib/mail/network/retriever_methods/pop3.rb', line 46 def settings @settings end |
Instance Method Details
#all(options = {}, &block) ⇒ Object
Get all emails.
Possible options:
order: order of emails returned. Possible values are :asc or :desc. Default value is :asc.
79 80 81 82 83 |
# File 'lib/mail/network/retriever_methods/pop3.rb', line 79 def all( = {}, &block) ||= {} [:count] = :all find(, &block) end |
#find(options = {}, &block) ⇒ Object
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.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/mail/network/retriever_methods/pop3.rb', line 93 def find( = {}, &block) = () start do |pop3| mails = pop3.mails mails.sort! { |m1, m2| m2.number <=> m1.number } if [:what] == :last mails = mails.first([:count]) if [:count].is_a? Integer if [:what].to_sym == :last && [:order].to_sym == :desc || [:what].to_sym == :first && [:order].to_sym == :asc || mails.reverse! end if block_given? mails.each do |mail| yield Mail.new(mail.pop) end else emails = [] mails.each do |mail| emails << Mail.new(mail.pop) end emails.size == 1 && [:count] == 1 ? emails.first : emails end end end |
#first(options = {}, &block) ⇒ Object
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.
54 55 56 57 58 59 |
# File 'lib/mail/network/retriever_methods/pop3.rb', line 54 def first( = {}, &block) ||= {} [:what] = :first [:count] ||= 1 find(, &block) end |
#last(options = {}, &block) ⇒ Object
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.
67 68 69 70 71 72 |
# File 'lib/mail/network/retriever_methods/pop3.rb', line 67 def last( = {}, &block) ||= {} [:what] = :last [:count] ||= 1 find(, &block) end |