mail 1.3.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of mail might be problematic. Click here for more details.

data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,22 @@
1
+ == Sun Dec 13 01:06:17 UTC 2009 Mikel Lindsaar <raasdnil@gmail.com>
2
+
3
+ * Version 1.3.1
4
+ * Resolved Issue #18 - Wrong instance variable name
5
+ * Resolved Issue #15 - Duplicate block call
6
+
7
+ == Thu Dec 10 21:25:37 UTC 2009 Mikel Lindsaar <raasdnil@gmail.com>
8
+
9
+ * Resolved Issue #13 - replacing From field results in from field becoming optional field.
10
+
11
+ == Thu 3 Dec 2009 00:52:12 UTC Mikel Lindsaar <raasdnil@gmail.com>
12
+
13
+ * Added POP upgrades from Nicolas Fouché
14
+ * Added patch to handle multiple from lines in email from Luke Grimstrup
15
+
16
+ == Mon Nov 23 23:34:22 UTC 2009 Mikel Lindsaar <raasdnil@gmail.com>
17
+
18
+ * Resolved Issue #12 - Wrong comment in smtp.rb
19
+
1
20
  == Mon Nov 23 22:35:50 UTC 2009 Mikel Lindsaar <raasdnil@gmail.com>
2
21
 
3
22
  * Changed the way attachments are added so that it does not break depending on the order
data/README.rdoc CHANGED
@@ -229,18 +229,52 @@ or
229
229
 
230
230
  mail.deliver!
231
231
 
232
- === Getting an email from a pop server:
232
+ === Getting emails from a pop server:
233
+
234
+ The most recent email:
233
235
 
234
236
  require 'mail'
235
237
 
236
238
  Mail.defaults do
237
- pop3 'mail.myhost.com.au', 110
238
- user 'mikel'
239
- pass 'mypass'
239
+ pop3 'mail.myhost.com.au', 110 do
240
+ user 'mikel'
241
+ pass 'mypass'
242
+ end
240
243
  end
241
244
 
242
- emails = Mail.get_all_mail
243
-
245
+ emails = Mail.last do |email|
246
+ email.reply!("I'll be back on Monday, please be patient!") # +reply!+ still not implemented ^^
247
+ end
248
+
249
+ The 3 oldest emails sorted by date in descendent order:
250
+
251
+ require 'mail'
252
+
253
+ Mail.defaults do
254
+ pop3 'mail.myhost.co.jp', 995 do
255
+ user 'mikel'
256
+ pass 'mypass'
257
+ enable_tls
258
+ end
259
+ end
260
+
261
+ emails = Mail.first(:count => 3, :order => :desc) do |email|
262
+ email.date
263
+ end
264
+
265
+ Or even all emails:
266
+
267
+ require 'mail'
268
+
269
+ Mail.defaults do
270
+ pop3 'mail.myhost.com.au' do
271
+ user 'mikel'
272
+ pass 'mypass'
273
+ end
274
+ end
275
+
276
+ emails = Mail.all
277
+
244
278
  emails.length #=> 12
245
279
 
246
280
  === Reading an Email
data/Rakefile CHANGED
@@ -12,7 +12,7 @@ require 'bundler'
12
12
 
13
13
  spec = Gem::Specification.new do |s|
14
14
  s.name = "mail"
15
- s.version = "1.3.0"
15
+ s.version = "1.3.1"
16
16
  s.author = "Mike Lindsaar"
17
17
  s.email = "raasdnil@gmail.com"
18
18
  s.homepage = "http://github.com/mikel/mail"
data/lib/mail/body.rb CHANGED
@@ -54,10 +54,39 @@ module Mail
54
54
  if other.class == String
55
55
  self.decoded == other
56
56
  else
57
- self.eql?(other)
57
+ super
58
58
  end
59
59
  end
60
60
 
61
+ # Accepts a string and performs a regular expression against the decoded text
62
+ #
63
+ # Examples:
64
+ #
65
+ # body = Mail::Body.new('The body')
66
+ # body =~ /The/ #=> 0
67
+ #
68
+ # body = Mail::Body.new("VGhlIGJvZHk=\n")
69
+ # body.encoding = 'base64'
70
+ # body =~ /The/ #=> 0
71
+ def =~(regexp)
72
+ self.decoded =~ regexp
73
+ end
74
+
75
+ # Accepts a string and performs a regular expression against the decoded text
76
+ #
77
+ # Examples:
78
+ #
79
+ # body = Mail::Body.new('The body')
80
+ # body.match(/The/) #=> #<MatchData "The">
81
+ #
82
+ # body = Mail::Body.new("VGhlIGJvZHk=\n")
83
+ # body.encoding = 'base64'
84
+ # body.match(/The/) #=> #<MatchData "The">
85
+ def match(regexp)
86
+ self.decoded.match(regexp)
87
+ end
88
+
89
+
61
90
  # Returns the raw source that the body was initialized with, without
62
91
  # any tampering
63
92
  def raw_source
@@ -67,7 +67,9 @@ module Mail
67
67
  # end
68
68
  #
69
69
  # Mail.defaults do
70
- # pop3 '127.0.0.1', 110
70
+ # pop3 '127.0.0.1', 995 do
71
+ # enable_tls
72
+ # end
71
73
  # end
72
74
  def pop3(*args, &block)
73
75
  if args.size > 0
@@ -104,9 +106,6 @@ module Mail
104
106
  port host_array[1]
105
107
  end
106
108
  end
107
- if block_given?
108
- klass.instance.settings(&block)
109
- end
110
109
  klass.instance.settings(&block)
111
110
  end
112
111
 
data/lib/mail/field.rb CHANGED
@@ -149,7 +149,7 @@ module Mail
149
149
  def new_field(name, value)
150
150
  # Could do this with constantize and make it "as DRY as", but a simple case
151
151
  # statement is, well, simpler...
152
- case name
152
+ case name.to_s
153
153
  when /^to$/i
154
154
  ToField.new(name, value)
155
155
  when /^cc$/i
@@ -21,7 +21,9 @@
21
21
  # mail['bcc'] #=> '#<Mail::Field:0x180e5e8 @field=#<Mail::BccField:0x180e1c4
22
22
  # mail['Bcc'] #=> '#<Mail::Field:0x180e5e8 @field=#<Mail::BccField:0x180e1c4
23
23
  #
24
- # mail.bcc.to_s #=> 'Mikel Lindsaar <mikel@test.lindsaar.net>, ada@test.lindsaar.net'
24
+ # mail.bcc.to_s #=> '' # Bcc field does not get output into an email
25
+ # mail.bcc.encoded #=> '' # Bcc field does not get output into an email
26
+ # mail.bcc.decoded #=> 'Mikel Lindsaar <mikel@test.lindsaar.net>, ada@test.lindsaar.net'
25
27
  # mail.bcc.addresses #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
26
28
  # mail.bcc.formatted #=> ['Mikel Lindsaar <mikel@test.lindsaar.net>', 'ada@test.lindsaar.net']
27
29
  #
data/lib/mail/mail.rb CHANGED
@@ -123,16 +123,35 @@ module Mail
123
123
  mail
124
124
  end
125
125
 
126
- # Returns all the messages on the server using the retriever method set up
127
- # in Mail.defaults
128
- def Mail.get_all_mail(&block)
129
- if block_given?
130
- Retrievable.get_messages.each do |message|
131
- yield message
132
- end
133
- else
134
- Retrievable.get_messages
135
- end
126
+ # Receive the first email(s) from a Pop3 server.
127
+ # See Mail::POP3 for a complete documentation.
128
+ def Mail.first(*args, &block)
129
+ Retrievable.first(*args, &block)
130
+ end
131
+
132
+ # Receive the first email(s) from a Pop3 server.
133
+ # See Mail::POP3 for a complete documentation.
134
+ def Mail.last(*args, &block)
135
+ Retrievable.last(*args, &block)
136
+ end
137
+
138
+ # Receive all emails from a Pop3 server.
139
+ # See Mail::POP3 for a complete documentation.
140
+ def Mail.all(*args, &block)
141
+ Retrievable.all(*args, &block)
142
+ end
143
+
144
+ # Receive all emails from a Pop3 server.
145
+ # DEPRECATED: please use Mail.all instead.
146
+ def Mail.get_all_mail(*args, &block)
147
+ warn "Mail.get_all_mail is deprecated. Please use Mail.all instead."
148
+ all(*args, &block)
149
+ end
150
+
151
+ # Find emails in a Pop3 server.
152
+ # See Mail::POP3 for a complete documentation.
153
+ def Mail.find(*args, &block)
154
+ Retrievable.find(*args, &block)
136
155
  end
137
156
 
138
157
  def Mail.read(filename)
data/lib/mail/message.rb CHANGED
@@ -808,7 +808,7 @@ module Mail
808
808
  end
809
809
 
810
810
  def set_envelope_header
811
- if match_data = raw_source.to_s.match(/From\s(#{TEXT}+)#{CRLF}(.*)/m)
811
+ if match_data = raw_source.to_s.match(/^From\s(#{TEXT}+)#{CRLF}(.*)/m)
812
812
  set_envelope(match_data[1])
813
813
  self.raw_source = match_data[2]
814
814
  end
@@ -920,10 +920,5 @@ module Mail
920
920
  filename
921
921
  end
922
922
 
923
- # Only POP3 is supported for now
924
- def Message.get_all_mail(&block)
925
- self.pop3_get_all_mail(&block)
926
- end
927
-
928
923
  end
929
924
  end
@@ -32,7 +32,7 @@ module Mail
32
32
  # The helo domain used at the begining of an SMTP conversation,
33
33
  # default is 'localhost.localdomain'
34
34
  def helo(value = nil)
35
- value ? @help = value : @helo ||= 'localhost.localdomain'
35
+ value ? @helo = value : @helo ||= 'localhost.localdomain'
36
36
  end
37
37
 
38
38
  # Turn on TLS
@@ -40,7 +40,7 @@ module Mail
40
40
  @tls = true
41
41
  end
42
42
 
43
- # Turn on TLS
43
+ # Turn off TLS
44
44
  def disable_tls
45
45
  @tls = false
46
46
  end
@@ -8,10 +8,12 @@
8
8
  module Mail
9
9
  module Retrievable
10
10
 
11
- def self.get_messages
12
- Mail.defaults.retriever_method.get_messages
11
+ module_function
12
+
13
+ def method_missing(name, *args, &block)
14
+ Mail.defaults.retriever_method.__send__(name, *args, &block)
13
15
  end
14
-
16
+
15
17
  end
16
-
18
+
17
19
  end
@@ -1,3 +1,12 @@
1
+ # encoding: utf-8
2
+
3
+ # The Pop3 retriever allows to get the last, first or all emails from a Pop3 server.
4
+ # Each email retrieved (RFC2822) is given as an instance of +Message+.
5
+ #
6
+ # While being retrieved, emails can be yielded if a block is given.
7
+ #
8
+ # This module uses the defaults set in Configuration to retrieve POP3 settings.
9
+ #
1
10
  module Mail
2
11
  class POP3
3
12
  include Singleton
@@ -46,40 +55,107 @@ module Mail
46
55
  @tls || false
47
56
  end
48
57
 
49
- # Get all emails via POP3.
50
- # TODO :limit option
51
- # TODO :order option
52
- def POP3.get_messages(&block)
53
- config = Mail.defaults
54
- if config.pop3.host.blank? || config.pop3.port.blank?
55
- raise ArgumentError.new('Please call +Mail.defaults+ to set the POP3 configuration')
56
- end
58
+ # Get the oldest received email(s)
59
+ #
60
+ # Possible options:
61
+ # count: number of emails to retrieve. The default value is 1.
62
+ # order: order of emails returned. Possible values are :asc or :desc. Default value is :asc.
63
+ #
64
+ def POP3.first(options = {}, &block)
65
+ options ||= {}
66
+ options[:what] = :first
67
+ options[:count] ||= 1
68
+ find(options, &block)
69
+ end
70
+
71
+ # Get the most recent received email(s)
72
+ #
73
+ # Possible options:
74
+ # count: number of emails to retrieve. The default value is 1.
75
+ # order: order of emails returned. Possible values are :asc or :desc. Default value is :asc.
76
+ #
77
+ def POP3.last(options = {}, &block)
78
+ options ||= {}
79
+ options[:what] = :last
80
+ options[:count] ||= 1
81
+ find(options, &block)
82
+ end
83
+
84
+ # Get all emails.
85
+ #
86
+ # Possible options:
87
+ # order: order of emails returned. Possible values are :asc or :desc. Default value is :asc.
88
+ #
89
+ def POP3.all(options = {}, &block)
90
+ options ||= {}
91
+ options[:count] = :all
92
+ find(options, &block)
93
+ end
94
+
95
+ # Find emails in a POP3 mailbox. Without any options, the 5 last received emails are returned.
96
+ #
97
+ # Possible options:
98
+ # what: last or first emails. The default is :first.
99
+ # order: order of emails returned. Possible values are :asc or :desc. Default value is :asc.
100
+ # count: number of emails to retrieve. The default value is 10. A value of 1 returns an
101
+ # instance of Message, not an array of Message instances.
102
+ #
103
+ def POP3.find(options = {}, &block)
104
+ validate_configuration
105
+ options = validate_options(options)
57
106
 
58
107
  start do |pop3|
108
+ mails = pop3.mails
109
+ mails.sort! { |m1, m2| m2.number <=> m1.number } if options[:what] == :last
110
+ mails = mails.first(options[:count]) if options[:count].is_a? Integer
111
+
112
+ if options[:what].to_sym == :last && options[:order].to_sym == :desc ||
113
+ options[:what].to_sym == :first && options[:order].to_sym == :asc ||
114
+ mails.reverse!
115
+ end
116
+
59
117
  if block_given?
60
- pop3.each_mail do |pop_mail|
61
- yield Mail.new(pop_mail.pop)
118
+ mails.each do |mail|
119
+ yield Mail.new(mail.pop)
62
120
  end
63
121
  else
64
122
  emails = []
65
- pop3.each_mail do |pop_mail|
66
- emails << Mail.new(pop_mail.pop)
123
+ mails.each do |mail|
124
+ emails << Mail.new(mail.pop)
67
125
  end
68
- emails
126
+ emails.size == 1 && options[:count] == 1 ? emails.first : emails
69
127
  end
128
+
70
129
  end
71
-
72
130
  end
73
131
 
74
- private
132
+ private
75
133
 
134
+ # Ensure that the POP3 configuration is set
135
+ def POP3.validate_configuration
136
+ config = Mail::Configuration.instance
137
+ if config.pop3.host.blank? || config.pop3.port.blank?
138
+ raise ArgumentError.new('Please call +Mail.defaults+ to set the POP3 configuration')
139
+ end
140
+ end
141
+
142
+ # Set default options
143
+ def POP3.validate_options(options)
144
+ options ||= {}
145
+ options[:count] ||= 10
146
+ options[:order] ||= :asc
147
+ options[:what] ||= :first
148
+ options
149
+ end
150
+
151
+ # Start a POP3 session and ensures that it will be closed in any case.
76
152
  def POP3.start(config = Mail::Configuration.instance, &block)
77
153
  raise ArgumentError.new("Mail::Retrievable#pop3_start takes a block") unless block_given?
78
-
154
+
79
155
  pop3 = Net::POP3.new(config.pop3.host, config.pop3.port, isapop = false)
80
156
  pop3.enable_ssl(verify = OpenSSL::SSL::VERIFY_NONE) if config.pop3.tls?
81
157
  pop3.start(config.pop3.user, config.pop3.pass)
82
-
158
+
83
159
  yield pop3
84
160
  ensure
85
161
  if defined?(pop3) && pop3 && pop3.started?
data/lib/mail/version.rb CHANGED
@@ -3,7 +3,7 @@ module Mail
3
3
  module VERSION
4
4
  MAJOR = 1
5
5
  MINOR = 3
6
- TINY = 0
6
+ TINY = 1
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY].join('.')
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mail
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Lindsaar
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-24 00:00:00 +11:00
12
+ date: 2009-12-13 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency