pony 0.9.1 → 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.rdoc +53 -34
  2. data/lib/pony.rb +186 -81
  3. data/pony.gemspec +2 -3
  4. data/spec/pony_spec.rb +98 -119
  5. metadata +8 -22
@@ -5,8 +5,8 @@
5
5
  Ruby no longer has to be jealous of PHP's mail() function, which can send an email in a single command.
6
6
 
7
7
  Pony.mail(:to => 'you@example.com', :from => 'me@example.com', :subject => 'hi', :body => 'Hello there.')
8
- Pony.mail(:to => 'you@example.com', :body => '<h1>Hello there!</h1>', :content_type => 'text/html')
9
- Pony.mail(:to => 'you@example.com', :cc => 'him@example.com', :from => 'me@example.com', :subject => 'hi', :body => 'Howdy')
8
+ Pony.mail(:to => 'you@example.com', :html_body => '<h1>Hello there!</h1>', :body => "In case you can't read html, Hello there.")
9
+ Pony.mail(:to => 'you@example.com', :cc => 'him@example.com', :from => 'me@example.com', :subject => 'hi', :body => 'Howsit!')
10
10
 
11
11
  Any option key may be omitted except for :to. For a complete list of options, see List Of Options section below.
12
12
 
@@ -15,7 +15,7 @@ Any option key may be omitted except for :to. For a complete list of options, se
15
15
 
16
16
  Pony uses /usr/sbin/sendmail to send mail if it is available, otherwise it uses SMTP to localhost.
17
17
 
18
- This can be over-ridden if you specify a via option
18
+ This can be over-ridden if you specify a via option:
19
19
 
20
20
  Pony.mail(:to => 'you@example.com', :via => :smtp) # sends via SMTP
21
21
 
@@ -23,29 +23,34 @@ This can be over-ridden if you specify a via option
23
23
 
24
24
  You can also specify options for SMTP:
25
25
 
26
- Pony.mail(:to => 'you@example.com', :via => :smtp, :smtp => {
27
- :host => 'smtp.yourserver.com',
28
- :port => '25',
29
- :user => 'user',
30
- :password => 'password',
31
- :auth => :plain, # :plain, :login, :cram_md5, no auth by default
32
- :domain => "localhost.localdomain" # the HELO domain provided by the client to the server
26
+ Pony.mail(:to => 'you@example.com', :via => :smtp, :via_options => {
27
+ :address => 'smtp.yourserver.com',
28
+ :port => '25',
29
+ :user_name => 'user',
30
+ :password => 'password',
31
+ :authentication => :plain, # :plain, :login, :cram_md5, no auth by default
32
+ :domain => "localhost.localdomain" # the HELO domain provided by the client to the server
33
33
  }
34
34
 
35
- == TLS/SSL
35
+ Gmail example (with TLS/SSL)
36
36
 
37
- With smtp transport it also possible to use TLS/SSL:
38
-
39
- Pony.mail(:to => 'you@example.com', :via => :smtp, :smtp => {
40
- :host => 'smtp.gmail.com',
41
- :port => '587',
42
- :tls => true,
43
- :user => 'user',
44
- :password => 'password',
45
- :auth => :plain, # :plain, :login, :cram_md5, no auth by default
46
- :domain => "localhost.localdomain" # the HELO domain provided by the client to the server
37
+ Pony.mail(:to => 'you@example.com', :via => :smtp, :via_options => {
38
+ :address => 'smtp.gmail.com',
39
+ :port => '587',
40
+ :enable_starttls_auto => true,
41
+ :user_name => 'user',
42
+ :password => 'password',
43
+ :authentication => :plain, # :plain, :login, :cram_md5, no auth by default
44
+ :domain => "localhost.localdomain" # the HELO domain provided by the client to the server
47
45
  })
48
46
 
47
+ And options for Sendmail:
48
+
49
+ Pony.mail(:to => 'you@example.com', :via => :smtp, :via_options => {
50
+ :location => '/path/to/sendmail' # this defaults to 'which sendmail' or '/usr/sbin/sendmail' if 'which' fails
51
+ :arguments => '-t' # -t and -i are the defaults
52
+ }
53
+
49
54
  == Attachments
50
55
 
51
56
  You can attach a file or two with the :attachments option:
@@ -62,32 +67,34 @@ Pony allows you to specify custom mail headers
62
67
  :headers => { "List-ID" => "...", "X-My-Custom-Header" => "what a cool custom header" }
63
68
  )
64
69
 
65
-
66
70
  == List Of Options
67
71
 
68
- Options passed pretty much directly to Tmail
69
- content_type
72
+ Options passed pretty much directly to Mail
70
73
  to
71
74
  cc
72
75
  bcc
73
76
  from
77
+ body # the plain text body
78
+ html_body # for sending html-formatted email
74
79
  subject
75
- charset
80
+ charset # In case you need to send in utf-8 or similar
76
81
  attachments # see Attachments section above
77
82
  headers # see Custom headers section above
78
83
  message_id
79
84
 
80
85
  Other options
81
86
  via # :smtp or :sendmail, see Transport section above
82
- smtp # specify smtp info, see Transport section above
83
- tls # use tls, see TLS/SSL section above
87
+ via_options # specify transport options, see Transport section above
88
+
89
+ == Help
90
+
91
+ If you need help using Pony, or it looks like you've found a bug, we have a google group setup at: ponyrb@googlegroups.com.
84
92
 
85
93
  == External Dependencies
86
94
 
87
- tmail ~> 1.0
88
- mime-types >= 1.16
95
+ mail > 2.0
89
96
 
90
- Note: these requirements are specified in the gemspec as well. Also, you may need smtp_tls if you want to send via tls and are using ruby < 1.8.7
97
+ Note: these requirements are specified in the gemspec as well. Also, you may need smtp_tls if you want to send via tls/ssl and are using ruby < 1.8.7
91
98
 
92
99
  == Meta
93
100
 
@@ -95,9 +102,18 @@ Maintained by Ben Prew
95
102
 
96
103
  Written by Adam Wiggins
97
104
 
98
- Patches contributed by: Mathieu Martin, Arun Thampi, Thomas Hurst,
99
- Stephen Celis, Othmane Benkirane, Neil Mock, Hiroshi Saito, Jesse
100
- Cooke, Nickolas Means, and Rick Olson
105
+ Patches contributed by:
106
+ Arun Thampi,
107
+ Hiroshi Saito,
108
+ Jesse Cooke,
109
+ Mathieu Martin,
110
+ Neil Mock,
111
+ Nickolas Means,
112
+ Othmane Benkirane,
113
+ Rick Olson
114
+ Stephen Celis,
115
+ Thomas Hurst,
116
+ Kalin Harvey
101
117
 
102
118
  Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
103
119
 
@@ -107,13 +123,16 @@ mailing list: ponyrb@googlegroups.com
107
123
 
108
124
  == Releases
109
125
 
126
+ 1.0
127
+ * Convert to using Mail as the mail-generation backend, instead of TMail
128
+
110
129
  0.9.1
111
130
  * provide the ability to set custom mail headers with something like:
112
131
  Pony.mail(:headers => {"List-ID" => "..."})
113
132
  * provide the ability to set the Message-Id from Pony.mail
114
133
 
115
134
  0.9
116
- * merge in kalins fixes to use tmail.destinations instead of trying to parse tmail.to, tmail.cc and tmail.bcc. New specs to test functionality
135
+ * merge in kalin's fixes to use tmail.destinations instead of trying to parse tmail.to, tmail.cc and tmail.bcc. New specs to test functionality
117
136
 
118
137
  0.8
119
138
  * Fix bug that was allowing nil :bcc and :cc options to be passed to smtp
@@ -1,65 +1,199 @@
1
1
  require 'rubygems'
2
- require 'net/smtp'
3
- require 'mime/types'
4
- begin
5
- require 'smtp_tls'
6
- rescue LoadError
7
- end
2
+ require 'mail'
8
3
  require 'base64'
9
- begin
10
- require 'tmail'
11
- rescue LoadError
12
- require 'actionmailer'
13
- end
4
+
5
+ # = Pony, the express way to send email in Ruby
6
+ #
7
+ # == Overview
8
+ #
9
+ # Ruby no longer has to be jealous of PHP's mail() function, which can send an email in a single command.
10
+ #
11
+ # Pony.mail(:to => 'you@example.com', :from => 'me@example.com', :subject => 'hi', :body => 'Hello there.')
12
+ # Pony.mail(:to => 'you@example.com', :html_body => '<h1>Hello there!</h1>', :body => "In case you can't read html, Hello there.")
13
+ # Pony.mail(:to => 'you@example.com', :cc => 'him@example.com', :from => 'me@example.com', :subject => 'hi', :body => 'Howsit!')
14
+ #
15
+ # Any option key may be omitted except for :to. For a complete list of options, see List Of Options section below.
16
+ #
17
+ #
18
+ # == Transport
19
+ #
20
+ # Pony uses /usr/sbin/sendmail to send mail if it is available, otherwise it uses SMTP to localhost.
21
+ #
22
+ # This can be over-ridden if you specify a via option:
23
+ #
24
+ # Pony.mail(:to => 'you@example.com', :via => :smtp) # sends via SMTP
25
+ #
26
+ # Pony.mail(:to => 'you@example.com', :via => :sendmail) # sends via sendmail
27
+ #
28
+ # You can also specify options for SMTP:
29
+ #
30
+ # Pony.mail(:to => 'you@example.com', :via => :smtp, :via_options => {
31
+ # :address => 'smtp.yourserver.com',
32
+ # :port => '25',
33
+ # :user_name => 'user',
34
+ # :password => 'password',
35
+ # :authentication => :plain, # :plain, :login, :cram_md5, no auth by default
36
+ # :domain => "localhost.localdomain" # the HELO domain provided by the client to the server
37
+ # }
38
+ #
39
+ # Gmail example (with TLS/SSL)
40
+ #
41
+ # Pony.mail(:to => 'you@example.com', :via => :smtp, :via_options => {
42
+ # :address => 'smtp.gmail.com',
43
+ # :port => '587',
44
+ # :enable_starttls_auto => true,
45
+ # :user_name => 'user',
46
+ # :password => 'password',
47
+ # :authentication => :plain, # :plain, :login, :cram_md5, no auth by default
48
+ # :domain => "localhost.localdomain" # the HELO domain provided by the client to the server
49
+ # })
50
+ #
51
+ # And options for Sendmail:
52
+ #
53
+ # Pony.mail(:to => 'you@example.com', :via => :smtp, :via_options => {
54
+ # :location => '/path/to/sendmail' # this defaults to 'which sendmail' or '/usr/sbin/sendmail' if 'which' fails
55
+ # :arguments => '-t' # -t and -i are the defaults
56
+ # }
57
+ #
58
+ # == Attachments
59
+ #
60
+ # You can attach a file or two with the :attachments option:
61
+ #
62
+ # Pony.mail(..., :attachments => {"foo.zip" => File.read("path/to/foo.zip"), "hello.txt" => "hello!"})
63
+ #
64
+ # Note: An attachment's mime-type is set based on the filename (as dictated by the ruby gem mime-types). So 'foo.pdf' has a mime-type of 'application/pdf'
65
+ #
66
+ # == Custom Headers
67
+ #
68
+ # Pony allows you to specify custom mail headers
69
+ # Pony.mail(
70
+ # :to => 'me@example.com',
71
+ # :headers => { "List-ID" => "...", "X-My-Custom-Header" => "what a cool custom header" }
72
+ # )
73
+ #
74
+ # == List Of Options
75
+ #
76
+ # Options passed pretty much directly to Mail
77
+ # to
78
+ # cc
79
+ # bcc
80
+ # from
81
+ # body # the plain text body
82
+ # html_body # for sending html-formatted email
83
+ # subject
84
+ # charset # In case you need to send in utf-8 or similar
85
+ # attachments # see Attachments section above
86
+ # headers # see Custom headers section above
87
+ # message_id
88
+ #
89
+ # Other options
90
+ # via # :smtp or :sendmail, see Transport section above
91
+ # via_options # specify transport options, see Transport section above
92
+
93
+
14
94
 
15
95
  module Pony
96
+
97
+ # Send an email
98
+ # Pony.mail(:to => 'you@example.com', :from => 'me@example.com', :subject => 'hi', :body => 'Hello there.')
99
+ # Pony.mail(:to => 'you@example.com', :html_body => '<h1>Hello there!</h1>', :body => "In case you can't read html, Hello there.")
100
+ # Pony.mail(:to => 'you@example.com', :cc => 'him@example.com', :from => 'me@example.com', :subject => 'hi', :body => 'Howsit!')
16
101
  def self.mail(options)
17
102
  raise(ArgumentError, ":to is required") unless options[:to]
18
103
 
19
- via = options.delete(:via)
20
- if via.nil?
21
- transport build_tmail(options)
22
- else
23
- if via_options.include?(via.to_s)
24
- send("transport_via_#{via}", build_tmail(options), options)
25
- else
26
- raise(ArgumentError, ":via must be either smtp or sendmail")
104
+ options[:via] = default_delivery_method unless options.has_key?(:via)
105
+ raise(ArgumentError, ":via must be either smtp or sendmail") unless via_possibilities.include?(options[:via])
106
+
107
+ options = cross_reference_depricated_fields(options)
108
+
109
+ if options.has_key?(:via) && options[:via] == :sendmail
110
+ options[:via_options] ||= {}
111
+ options[:via_options][:location] ||= sendmail_binary
112
+ end
113
+
114
+ deliver build_mail(options)
115
+ end
116
+
117
+ private
118
+
119
+ def self.cross_reference_depricated_fields(options)
120
+ if options.has_key?(:smtp)
121
+ warn depricated_message(:smtp, :via_options)
122
+ options[:via_options] = options.delete(:smtp)
123
+ end
124
+
125
+ # cross-reference pony options to be compatible with keys mail expects
126
+ { :host => :address, :user => :user_name, :auth => :authentication, :tls => :enable_starttls_auto }.each do |key, val|
127
+ if options[:via_options] && options[:via_options].has_key?(key)
128
+ warn depricated_message(key, val)
129
+ options[:via_options][val] = options[:via_options].delete(key)
27
130
  end
28
131
  end
132
+
133
+ if options[:content_type] && options[:content_type] =~ /html/ && !options[:html_body]
134
+ warn depricated_message(:content_type, :html_body)
135
+ options[:html_body] = options[:body]
136
+ end
137
+
138
+ return options
29
139
  end
30
140
 
31
- def self.build_tmail(options)
32
- mail = TMail::Mail.new
33
- mail.to = options[:to]
34
- mail.cc = options[:cc]
35
- mail.bcc = options[:bcc]
36
- mail.from = options[:from] || 'pony@unknown'
37
- mail.subject = options[:subject]
38
- mail.message_id = options[:message_id]
39
- if options[:attachments]
40
- # If message has attachment, then body must be sent as a message part
41
- # or it will not be interpreted correctly by client.
42
- body = TMail::Mail.new
43
- body.body = options[:body] || ""
44
- body.content_type = options[:content_type] || "text/plain"
45
- mail.parts.push body
46
- (options[:attachments] || []).each do |name, body|
47
- attachment = TMail::Mail.new
48
- attachment.transfer_encoding = "base64"
49
- attachment.body = Base64.encode64(body)
50
- content_type = MIME::Types.type_for(name).to_s
51
- attachment.content_type = content_type unless content_type == ""
52
- attachment.set_content_disposition "attachment", "filename" => name
53
- mail.parts.push attachment
141
+ def self.deliver(mail)
142
+ mail.deliver!
143
+ end
144
+
145
+ def self.default_delivery_method
146
+ File.executable?(sendmail_binary) ? :sendmail : :smtp
147
+ end
148
+
149
+ def self.build_mail(options)
150
+ mail = Mail.new do
151
+ to options[:to]
152
+ from options[:from] || 'pony@unknown'
153
+ cc options[:cc]
154
+ bcc options[:bcc]
155
+ subject options[:subject]
156
+ date options[:date] || Time.now
157
+ message_id options[:message_id]
158
+
159
+ if options[:html_body]
160
+ html_part do
161
+ content_type 'text/html; charset=UTF-8'
162
+ body options[:html_body]
163
+ end
164
+ end
165
+
166
+ # If we're using attachments, the body needs to be a separate part. If not,
167
+ # we can just set the body directly.
168
+ if options[:body] && (options[:html_body] || options[:attachments])
169
+ text_part do
170
+ body options[:body]
171
+ end
172
+ elsif options[:body]
173
+ body options[:body]
174
+ end
175
+
176
+ delivery_method options[:via], (options.has_key?(:via_options) ? options[:via_options] : {})
177
+ end
178
+
179
+ (options[:attachments] || []).each do |name, body|
180
+ # mime-types wants to send these as "quoted-printable"
181
+ if name =~ /\.xlsx$/
182
+ mail.attachments[name] = {
183
+ :content => Base64.encode64(body),
184
+ :transfer_encoding => :base64
185
+ }
186
+ else
187
+ mail.attachments[name] = body
54
188
  end
55
- else
56
- mail.content_type = options[:content_type] || "text/plain"
57
- mail.body = options[:body] || ""
58
189
  end
190
+
59
191
  (options[:headers] ||= {}).each do |key, value|
60
192
  mail[key] = value
61
193
  end
62
- mail.charset = options[:charset] # charset must be set after setting content_type
194
+
195
+ mail.charset = options[:charset] if options[:charset] # charset must be set after setting content_type
196
+
63
197
  mail
64
198
  end
65
199
 
@@ -68,42 +202,13 @@ module Pony
68
202
  sendmail.empty? ? '/usr/sbin/sendmail' : sendmail
69
203
  end
70
204
 
71
- def self.transport(tmail)
72
- if File.executable? sendmail_binary
73
- transport_via_sendmail(tmail)
74
- else
75
- transport_via_smtp(tmail)
76
- end
77
- end
78
-
79
- def self.via_options
80
- %w(sendmail smtp)
81
- end
82
-
83
- def self.transport_via_sendmail(tmail, options={})
84
- IO.popen('-', 'w+') do |pipe|
85
- if pipe
86
- pipe.write(tmail.to_s)
87
- else
88
- exec(sendmail_binary, "-t")
89
- end
90
- end
205
+ def self.via_possibilities
206
+ %w(sendmail smtp).map{ |x| x.to_sym }
91
207
  end
92
208
 
93
- def self.transport_via_smtp(tmail, options={:smtp => {}})
94
- default_options = {:smtp => { :host => 'localhost', :port => '25', :domain => 'localhost.localdomain' }}
95
- o = default_options[:smtp].merge(options[:smtp])
96
- smtp = Net::SMTP.new(o[:host], o[:port])
97
- if o[:tls]
98
- raise "You may need: gem install smtp_tls" unless smtp.respond_to?(:enable_starttls)
99
- smtp.enable_starttls
100
- end
101
- if o.include?(:auth)
102
- smtp.start(o[:domain], o[:user], o[:password], o[:auth])
103
- else
104
- smtp.start(o[:domain])
105
- end
106
- smtp.send_message tmail.to_s, tmail.from, tmail.destinations
107
- smtp.finish
209
+ def self.depricated_message(method, alternative)
210
+ warning_message = "warning: '#{method}' is deprecated"
211
+ warning_message += "; use '#{alternative}' instead." if alternative
212
+ return warning_message
108
213
  end
109
214
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{pony}
5
- s.version = "0.9.1"
5
+ s.version = "1.0"
6
6
 
7
7
  s.description = "Send email in one command: Pony.mail(:to => 'someone@example.com', :body => 'hello')"
8
8
  s.summary = s.description
@@ -17,7 +17,6 @@ Gem::Specification.new do |s|
17
17
  s.rdoc_options = ["--inline-source", "--charset=UTF-8"]
18
18
  s.require_paths = ["lib"]
19
19
  s.rubygems_version = %q{1.3.1}
20
- s.add_dependency( 'tmail', '~> 1.0' )
21
- s.add_dependency('mime-types', '>= 1.16')
20
+ s.add_dependency( 'mail', '>2.0')
22
21
  s.platform = Gem::Platform::RUBY
23
22
  end
@@ -1,220 +1,192 @@
1
+ # -*- coding: utf-8 -*-
1
2
  require File.dirname(__FILE__) + '/base'
2
3
 
3
4
  describe Pony do
5
+
6
+ before(:each) do
7
+ Pony.stub!(:deliver)
8
+ end
9
+
4
10
  it "sends mail" do
5
- Pony.should_receive(:transport) do |tmail|
6
- tmail.to.should == [ 'joe@example.com' ]
7
- tmail.from.should == [ 'sender@example.com' ]
8
- tmail.subject.should == 'hi'
9
- tmail.body.should == 'Hello, Joe.'
11
+ Pony.should_receive(:deliver) do |mail|
12
+ mail.to.should == [ 'joe@example.com' ]
13
+ mail.from.should == [ 'sender@example.com' ]
14
+ mail.subject.should == 'hi'
15
+ mail.body.should == 'Hello, Joe.'
10
16
  end
11
17
  Pony.mail(:to => 'joe@example.com', :from => 'sender@example.com', :subject => 'hi', :body => 'Hello, Joe.')
12
18
  end
13
19
 
14
20
  it "requires :to param" do
15
- Pony.stub!(:transport)
16
21
  lambda { Pony.mail({}) }.should raise_error(ArgumentError)
17
22
  end
18
23
 
19
24
  it "doesn't require any other param" do
20
- Pony.stub!(:transport)
21
25
  lambda { Pony.mail(:to => 'joe@example.com') }.should_not raise_error
22
26
  end
23
27
 
28
+ it "should handle depricated options gracefully" do
29
+ Pony.should_receive(:build_mail).with(hash_including(:via_options => {:address => 'test'}))
30
+ Pony.mail(:to => 'foo@bar', :smtp => { :host => 'test' })
31
+ end
32
+
33
+ it "should handle depricated content-type gracefully" do
34
+ Pony.should_receive(:build_mail).with(hash_including(:html_body => "this is <h1>HTML</h1>"))
35
+ Pony.mail(:to => 'foo@bar', :content_type => 'text/html', :body => 'this is <h1>HTML</h1>')
36
+ end
24
37
  ####################
25
38
 
26
- describe "builds a TMail object with field:" do
39
+ describe "builds a Mail object with field:" do
27
40
  it "to" do
28
- Pony.build_tmail(:to => 'joe@example.com').to.should == [ 'joe@example.com' ]
41
+ Pony.build_mail(:to => 'joe@example.com').to.should == [ 'joe@example.com' ]
29
42
  end
30
43
 
31
44
  it "to with multiple recipients" do
32
- Pony.build_tmail(:to => 'joe@example.com, friedrich@example.com').to.should == [ 'joe@example.com', 'friedrich@example.com' ]
45
+ Pony.build_mail(:to => 'joe@example.com, friedrich@example.com').to.should == [ 'joe@example.com', 'friedrich@example.com' ]
33
46
  end
34
47
 
35
48
  it "to with multiple recipients and names" do
36
- Pony.build_tmail(:to => 'joe@example.com, "Friedrich Hayek" <friedrich@example.com>').to.should == [ 'joe@example.com', 'friedrich@example.com' ]
49
+ Pony.build_mail(:to => 'joe@example.com, "Friedrich Hayek" <friedrich@example.com>').to.should == [ 'joe@example.com', 'friedrich@example.com' ]
37
50
  end
38
51
 
39
52
  it "to with multiple recipients and names in an array" do
40
- Pony.build_tmail(:to => ['joe@example.com', '"Friedrich Hayek" <friedrich@example.com>']).to.should == [ 'joe@example.com', 'friedrich@example.com' ]
53
+ Pony.build_mail(:to => ['joe@example.com', '"Friedrich Hayek" <friedrich@example.com>']).to.should == [ 'joe@example.com', 'friedrich@example.com' ]
41
54
  end
42
55
 
43
56
  it "cc" do
44
- Pony.build_tmail(:cc => 'joe@example.com').cc.should == [ 'joe@example.com' ]
57
+ Pony.build_mail(:cc => 'joe@example.com').cc.should == [ 'joe@example.com' ]
45
58
  end
46
59
 
47
60
  it "cc with multiple recipients" do
48
- Pony.build_tmail(:cc => 'joe@example.com, friedrich@example.com').cc.should == [ 'joe@example.com', 'friedrich@example.com' ]
61
+ Pony.build_mail(:cc => 'joe@example.com, friedrich@example.com').cc.should == [ 'joe@example.com', 'friedrich@example.com' ]
49
62
  end
50
63
 
51
64
  it "from" do
52
- Pony.build_tmail(:from => 'joe@example.com').from.should == [ 'joe@example.com' ]
65
+ Pony.build_mail(:from => 'joe@example.com').from.should == [ 'joe@example.com' ]
53
66
  end
54
67
 
55
68
  it "bcc" do
56
- Pony.build_tmail(:bcc => 'joe@example.com').bcc.should == [ 'joe@example.com' ]
69
+ Pony.build_mail(:bcc => 'joe@example.com').bcc.should == [ 'joe@example.com' ]
57
70
  end
58
71
 
59
72
  it "bcc with multiple recipients" do
60
- Pony.build_tmail(:bcc => 'joe@example.com, friedrich@example.com').bcc.should == [ 'joe@example.com', 'friedrich@example.com' ]
73
+ Pony.build_mail(:bcc => 'joe@example.com, friedrich@example.com').bcc.should == [ 'joe@example.com', 'friedrich@example.com' ]
61
74
  end
62
75
 
63
76
  it "charset" do
64
- Pony.build_tmail(:charset => 'UTF-8').charset.should == 'UTF-8'
77
+ mail = Pony.build_mail(:charset => 'UTF-8')
78
+ mail.charset.should == 'UTF-8'
65
79
  end
66
80
 
67
81
  it "default charset" do
68
- Pony.build_tmail(:body => 'body').charset.should == nil
69
- Pony.build_tmail(:body => 'body', :content_type => 'text/html').charset.should == nil
82
+ Pony.build_mail(:body => 'body').charset.should == 'UTF-8'
83
+ Pony.build_mail(:body => 'body', :content_type => 'text/html').charset.should == 'UTF-8'
70
84
  end
71
85
 
72
86
  it "from (default)" do
73
- Pony.build_tmail({}).from.should == [ 'pony@unknown' ]
87
+ Pony.build_mail({}).from.should == [ 'pony@unknown' ]
74
88
  end
75
89
 
76
90
  it "subject" do
77
- Pony.build_tmail(:subject => 'hello').subject.should == 'hello'
91
+ Pony.build_mail(:subject => 'hello').subject.should == 'hello'
78
92
  end
79
93
 
80
94
  it "body" do
81
- Pony.build_tmail(:body => 'What do you know, Joe?').body.should == 'What do you know, Joe?'
95
+ Pony.build_mail(:body => 'What do you know, Joe?').body.should == 'What do you know, Joe?'
82
96
  end
83
97
 
84
- it "content_type" do
85
- Pony.build_tmail(:content_type => 'text/html').content_type.should == 'text/html'
98
+ it "html_body" do
99
+ Pony.build_mail(:html_body => 'What do you know, Joe?').parts.first.body.should == 'What do you know, Joe?'
100
+ Pony.build_mail(:html_body => 'What do you know, Joe?').parts.first.content_type.should == 'text/html; charset=UTF-8'
101
+ end
102
+
103
+ it "date" do
104
+ now = Time.now
105
+ Pony.build_mail(:date => now).date.should == DateTime.parse(now.to_s)
106
+ end
107
+
108
+ it "content_type of html should set html_body" do
109
+ Pony.should_receive(:build_mail).with(hash_including(:html_body => '<h1>test</h1>'))
110
+ Pony.mail(:to => 'foo@bar', :content_type => 'text/html', :body => '<h1>test</h1>')
86
111
  end
87
112
 
88
113
  it "message_id" do
89
- Pony.build_tmail(:message_id => '<abc@def.com>').message_id.should == '<abc@def.com>'
114
+ Pony.build_mail(:message_id => '<abc@def.com>').message_id.should == 'abc@def.com'
90
115
  end
91
116
 
92
117
  it "custom headers" do
93
- Pony.build_tmail(:headers => {"List-ID" => "<abc@def.com>"})['List-ID'].to_s.should == '<abc@def.com>'
118
+ Pony.build_mail(:headers => {"List-ID" => "<abc@def.com>"})['List-ID'].to_s.should == '<abc@def.com>'
94
119
  end
95
120
 
96
- it "attachments" do
97
- tmail = Pony.build_tmail(:attachments => {"foo.txt" => "content of foo.txt"})
98
- tmail.should have(2).parts
99
- tmail.parts.first.to_s.should == "Content-Type: text/plain\n\n"
100
- tmail.parts.last.to_s.should == <<-PART
101
- Content-Type: text/plain
102
- Content-Transfer-Encoding: Base64
103
- Content-Disposition: attachment; filename=foo.txt
121
+ it "utf-8 encoded subject line" do
122
+ mail = Pony.build_mail(:to => 'btp@foo', :subject => 'Café', :body => 'body body body')
123
+ mail['subject'].encoded.should =~ /^Subject: =\?UTF-8/;
124
+ end
104
125
 
105
- Y29udGVudCBvZiBmb28udHh0
106
- PART
126
+ it "attachments" do
127
+ mail = Pony.build_mail(:attachments => {"foo.txt" => "content of foo.txt"}, :body => 'test')
128
+ mail.parts.length.should == 2
129
+ mail.parts.first.to_s.should =~ /Content-Type: text\/plain/
107
130
  end
108
131
 
109
132
  it "suggests mime-type" do
110
- tmail = Pony.build_tmail(:attachments => {"foo.pdf" => "content of foo.pdf"})
111
- tmail.should have(2).parts
112
- tmail.parts.first.to_s.should == "Content-Type: text/plain\n\n"
113
- tmail.parts.last.to_s.should == <<-PART
114
- Content-Type: application/pdf
115
- Content-Transfer-Encoding: Base64
116
- Content-Disposition: attachment; filename=foo.pdf
133
+ mail = Pony.build_mail(:attachments => {"foo.pdf" => "content of foo.pdf"})
134
+ mail.parts.length.should == 1
135
+ mail.parts.first.to_s.should =~ /Content-Type: application\/pdf/
136
+ mail.parts.first.to_s.should =~ /filename=foo.pdf/
137
+ mail.parts.first.content_transfer_encoding.to_s.should == 'base64'
138
+ end
117
139
 
118
- Y29udGVudCBvZiBmb28ucGRm
119
- PART
140
+ it "encodes xlsx files as base64" do
141
+ mail = Pony.build_mail(:attachments => {"foo.xlsx" => "content of foo.xlsx"})
142
+ mail.parts.length.should == 1
143
+ mail.parts.first.to_s.should =~ /Content-Type: application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet/
144
+ mail.parts.first.to_s.should =~ /filename=foo.xlsx/
145
+ mail.parts.first.content_transfer_encoding.to_s.should == 'base64'
120
146
  end
121
- end
122
147
 
123
- describe "transport" do
124
- it "transports via the sendmail binary if it exists" do
125
- File.stub!(:executable?).and_return(true)
126
- Pony.should_receive(:transport_via_sendmail).with(:tmail)
127
- Pony.transport(:tmail)
148
+ it "passes cc and bcc as the list of recipients" do
149
+ mail = Pony.build_mail(:to => ['to'], :cc => ['cc'], :from => ['from'], :bcc => ['bcc'])
150
+ mail.destinations.should == ['to', 'cc', 'bcc']
128
151
  end
152
+ end
129
153
 
154
+ describe "transport" do
130
155
  it "transports via smtp if no sendmail binary" do
131
156
  Pony.stub!(:sendmail_binary).and_return('/does/not/exist')
132
- Pony.should_receive(:transport_via_smtp).with(:tmail)
133
- Pony.transport(:tmail)
157
+ Pony.should_receive(:build_mail).with(hash_including(:via => :smtp))
158
+ Pony.mail(:to => 'foo@bar')
134
159
  end
135
160
 
136
- it "transports mail via /usr/sbin/sendmail binary" do
137
- pipe = mock('sendmail pipe')
138
- IO.should_receive(:popen).with('-',"w+").and_yield(pipe)
139
- pipe.should_receive(:write).with('message')
140
- Pony.transport_via_sendmail(mock('tmail', :to => ['to'], :from => ['from'], :to_s => 'message'))
161
+ it "defaults to sendmail if no via is specified and sendmail exists" do
162
+ File.stub!(:executable?).and_return(true)
163
+ Pony.should_receive(:build_mail).with(hash_including(:via => :sendmail))
164
+ Pony.mail(:to => 'foo@bar')
141
165
  end
142
166
 
143
167
  describe "SMTP transport" do
144
- before do
145
- @smtp = mock('net::smtp object')
146
- @smtp.stub!(:start)
147
- @smtp.stub!(:send_message)
148
- @smtp.stub!(:finish)
149
- Net::SMTP.stub!(:new).and_return(@smtp)
150
- end
151
-
152
- it "passes cc and bcc as the list of recipients" do
153
- @smtp.should_receive(:send_message).with("message", ['from'], ['to', 'cc', 'bcc'])
154
- Pony.transport_via_smtp(mock('tmail', :to => ['to'], :cc => ['cc'], :from => ['from'], :to_s => 'message', :bcc => ['bcc'], :destinations => ['to', 'cc', 'bcc']))
155
- @smtp.should_receive(:send_message).with("message", 'from', ['to', 'cc'])
156
- Pony.transport_via_smtp(mock('tmail', :to => 'to', :cc => 'cc', :from => 'from', :to_s => 'message', :bcc => nil, :destinations => ['to', 'cc']))
157
- end
158
-
159
- it "only pass cc as the list of recipients" do
160
- @smtp.should_receive(:send_message).with("message", ['from'], ['to', 'cc' ])
161
- Pony.transport_via_smtp(mock('tmail', :to => ['to'], :cc => ['cc'], :from => ['from'], :to_s => 'message', :bcc => nil, :destinations => ['to', 'cc']))
162
- end
163
-
164
- it "only pass bcc as the list of recipients" do
165
- @smtp.should_receive(:send_message).with("message", ['from'], ['to', 'bcc' ])
166
- Pony.transport_via_smtp(mock('tmail', :to => ['to'], :cc => nil, :from => ['from'], :to_s => 'message', :bcc => ['bcc'], :destinations => ['to', 'bcc']))
167
- end
168
-
169
- it "passes cc and bcc as the list of recipients when there are a few of them" do
170
- @smtp.should_receive(:send_message).with("message", ['from'], ['to', 'to2', 'cc', 'bcc', 'bcc2', 'bcc3'])
171
- Pony.transport_via_smtp(mock('tmail', :to => ['to', 'to2'], :cc => ['cc'], :from => ['from'], :to_s => 'message', :bcc => ['bcc', 'bcc2', 'bcc3'], :destinations => ['to', 'to2', 'cc', 'bcc', 'bcc2', 'bcc3']))
172
- end
173
168
 
174
169
  it "defaults to localhost as the SMTP server" do
175
- Net::SMTP.should_receive(:new).with('localhost', '25').and_return(@smtp)
176
- Pony.transport_via_smtp(mock('tmail', :to => ['to'], :from => ['from'], :to_s => 'message', :cc => nil, :bcc => nil, :destinations => ['to']))
177
- end
178
-
179
- it "uses SMTP authorization when auth key is provided" do
180
- o = { :smtp => { :user => 'user', :password => 'password', :auth => 'plain'}}
181
- @smtp.should_receive(:start).with('localhost.localdomain', 'user', 'password', 'plain')
182
- Pony.transport_via_smtp(mock('tmail', :to => ['to'], :from => ['from'], :to_s => 'message', :cc => nil, :bcc => nil, :destinations => ['to']), o)
170
+ mail = Pony.build_mail(:to => "foo@bar", :enable_starttls_auto => true, :via => :smtp)
171
+ mail.delivery_method.settings[:address].should == 'localhost'
183
172
  end
184
173
 
185
174
  it "enable starttls when tls option is true" do
186
- o = { :smtp => { :user => 'user', :password => 'password', :auth => 'plain', :tls => true}}
187
- @smtp.should_receive(:enable_starttls)
188
- Pony.transport_via_smtp(mock('tmail', :to => ['to'], :from => ['from'], :to_s => 'message', :cc => nil, :bcc => nil, :destinations => ['to']), o)
175
+ mail = Pony.build_mail(:to => "foo@bar", :enable_starttls_auto => true, :via => :smtp)
176
+ mail.delivery_method.settings[:enable_starttls_auto].should == true
189
177
  end
190
-
191
- it "starts the job" do
192
- @smtp.should_receive(:start)
193
- Pony.transport_via_smtp(mock('tmail', :to => ['to'], :from => ['from'], :to_s => 'message', :cc => nil, :bcc => nil, :destinations => ['to']))
194
- end
195
-
196
- it "sends a tmail message" do
197
- @smtp.should_receive(:send_message)
198
- Pony.transport_via_smtp(mock('tmail', :to => ['to'], :from => ['from'], :to_s => 'message', :cc => nil, :bcc => nil, :destinations => ['to']))
199
- end
200
-
201
- it "finishes the job" do
202
- @smtp.should_receive(:finish)
203
- Pony.transport_via_smtp(mock('tmail', :to => ['to'], :from => ['from'], :to_s => 'message', :cc => nil, :bcc => nil, :destinations => ['to']))
204
- end
205
-
206
178
  end
207
179
  end
208
180
 
209
181
  describe ":via option should over-ride the default transport mechanism" do
210
182
  it "should send via sendmail if :via => sendmail" do
211
- Pony.should_receive(:transport_via_sendmail)
212
- Pony.mail(:to => 'joe@example.com', :via => :sendmail)
183
+ mail = Pony.build_mail(:to => 'joe@example.com', :via => :sendmail)
184
+ mail.delivery_method.kind_of?(Mail::Sendmail).should == true
213
185
  end
214
186
 
215
187
  it "should send via smtp if :via => smtp" do
216
- Pony.should_receive(:transport_via_smtp)
217
- Pony.mail(:to => 'joe@example.com', :via => :smtp)
188
+ mail = Pony.build_mail(:to => 'joe@example.com', :via => :smtp)
189
+ mail.delivery_method.kind_of?(Mail::SMTP).should == true
218
190
  end
219
191
 
220
192
  it "should raise an error if via is neither smtp nor sendmail" do
@@ -222,6 +194,13 @@ Y29udGVudCBvZiBmb28ucGRm
222
194
  end
223
195
  end
224
196
 
197
+ describe "via_possibilities" do
198
+ it "should contain smtp and sendmail" do
199
+ Pony.via_possibilities.should == [:sendmail, :smtp]
200
+ end
201
+ end
202
+
203
+
225
204
  describe "sendmail binary location" do
226
205
  it "should default to /usr/sbin/sendmail if not in path" do
227
206
  Pony.stub!(:'`').and_return('')
metadata CHANGED
@@ -3,10 +3,9 @@ name: pony
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
- - 0
7
- - 9
8
6
  - 1
9
- version: 0.9.1
7
+ - 0
8
+ version: "1.0"
10
9
  platform: ruby
11
10
  authors:
12
11
  - Adam Wiggins
@@ -15,35 +14,22 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-04-15 00:00:00 -07:00
17
+ date: 2010-05-07 00:00:00 -06:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
- name: tmail
21
+ name: mail
23
22
  prerelease: false
24
23
  requirement: &id001 !ruby/object:Gem::Requirement
25
24
  requirements:
26
- - - ~>
25
+ - - ">"
27
26
  - !ruby/object:Gem::Version
28
27
  segments:
29
- - 1
28
+ - 2
30
29
  - 0
31
- version: "1.0"
30
+ version: "2.0"
32
31
  type: :runtime
33
32
  version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: mime-types
36
- prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- segments:
42
- - 1
43
- - 16
44
- version: "1.16"
45
- type: :runtime
46
- version_requirements: *id002
47
33
  description: "Send email in one command: Pony.mail(:to => 'someone@example.com', :body => 'hello')"
48
34
  email: ben.prew@gmail.com
49
35
  executables: []
@@ -57,8 +43,8 @@ files:
57
43
  - Rakefile
58
44
  - pony.gemspec
59
45
  - lib/pony.rb
60
- - spec/base.rb
61
46
  - spec/pony_spec.rb
47
+ - spec/base.rb
62
48
  has_rdoc: true
63
49
  homepage: http://github.com/benprew/pony
64
50
  licenses: []