Browse files

add bundler, move from tabs to soft tabs (2 spaces) and add reply_to …

…option
  • Loading branch information...
1 parent e5f5f1e commit bb4c152124100f3533402160d89b5903a1b77c85 @danielvlopes danielvlopes committed with Mar 23, 2011
Showing with 410 additions and 364 deletions.
  1. +3 −0 Gemfile
  2. +37 −0 Gemfile.lock
  3. +120 −119 lib/pony.rb
  4. +17 −16 pony.gemspec
  5. +233 −229 spec/pony_spec.rb
View
3 Gemfile
@@ -0,0 +1,3 @@
+source "http://rubygems.org"
+
+gemspec
View
37 Gemfile.lock
@@ -0,0 +1,37 @@
+PATH
+ remote: .
+ specs:
+ pony (1.1)
+ mail (> 2.0)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ activesupport (3.0.5)
+ diff-lcs (1.1.2)
+ i18n (0.5.0)
+ mail (2.2.15)
+ activesupport (>= 2.3.6)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.16)
+ polyglot (0.3.1)
+ rspec (2.5.0)
+ rspec-core (~> 2.5.0)
+ rspec-expectations (~> 2.5.0)
+ rspec-mocks (~> 2.5.0)
+ rspec-core (2.5.1)
+ rspec-expectations (2.5.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.5.0)
+ treetop (1.4.9)
+ polyglot (>= 0.3.1)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ mail (> 2.0)
+ pony!
+ rspec (>= 2.0.0)
View
239 lib/pony.rb
@@ -102,138 +102,139 @@
module Pony
- @@options = {}
+ @@options = {}
# Default options can be set so that they don't have to be repeated.
#
# Pony.options = { :from => 'noreply@example.com', :via => :smtp, :via_options => { :host => 'smtp.yourserver.com' } }
# Pony.mail(:to => 'foo@bar') # Sends mail to foo@bar from noreply@example.com using smtp
# Pony.mail(:from => 'pony@example.com', :to => 'foo@bar') # Sends mail to foo@bar from pony@example.com using smtp
- def self.options=(value)
- @@options = value
- end
+ def self.options=(value)
+ @@options = value
+ end
- def self.options()
- @@options
- end
+ def self.options()
+ @@options
+ end
# Send an email
# Pony.mail(:to => 'you@example.com', :from => 'me@example.com', :subject => 'hi', :body => 'Hello there.')
# Pony.mail(:to => 'you@example.com', :html_body => '<h1>Hello there!</h1>', :body => "In case you can't read html, Hello there.")
# Pony.mail(:to => 'you@example.com', :cc => 'him@example.com', :from => 'me@example.com', :subject => 'hi', :body => 'Howsit!')
- def self.mail(options)
- options = @@options.merge options
- raise(ArgumentError, ":to is required") unless options[:to]
-
- options[:via] = default_delivery_method unless options.has_key?(:via)
- raise(ArgumentError, ":via must be either smtp or sendmail") unless via_possibilities.include?(options[:via])
-
- options = cross_reference_depricated_fields(options)
-
- if options.has_key?(:via) && options[:via] == :sendmail
- options[:via_options] ||= {}
- options[:via_options][:location] ||= sendmail_binary
- end
-
- deliver build_mail(options)
- end
-
- private
-
- def self.cross_reference_depricated_fields(options)
- if options.has_key?(:smtp)
- warn depricated_message(:smtp, :via_options)
- options[:via_options] = options.delete(:smtp)
- end
-
- # cross-reference pony options to be compatible with keys mail expects
- { :host => :address, :user => :user_name, :auth => :authentication, :tls => :enable_starttls_auto }.each do |key, val|
- if options[:via_options] && options[:via_options].has_key?(key)
- warn depricated_message(key, val)
- options[:via_options][val] = options[:via_options].delete(key)
- end
- end
-
- if options[:content_type] && options[:content_type] =~ /html/ && !options[:html_body]
- warn depricated_message(:content_type, :html_body)
- options[:html_body] = options[:body]
- end
-
- return options
- end
-
- def self.deliver(mail)
- mail.deliver!
- end
-
- def self.default_delivery_method
- File.executable?(sendmail_binary) ? :sendmail : :smtp
- end
-
- def self.build_mail(options)
- mail = Mail.new do
- to options[:to]
- from options[:from] || 'pony@unknown'
- cc options[:cc]
- bcc options[:bcc]
- subject options[:subject]
- date options[:date] || Time.now
- message_id options[:message_id]
- sender options[:sender] if options[:sender]
-
- if options[:html_body]
- html_part do
- content_type 'text/html; charset=UTF-8'
- body options[:html_body]
- end
- end
-
- # If we're using attachments, the body needs to be a separate part. If not,
+ def self.mail(options)
+ options = @@options.merge options
+ raise(ArgumentError, ":to is required") unless options[:to]
+
+ options[:via] = default_delivery_method unless options.has_key?(:via)
+ raise(ArgumentError, ":via must be either smtp or sendmail") unless via_possibilities.include?(options[:via])
+
+ options = cross_reference_depricated_fields(options)
+
+ if options.has_key?(:via) && options[:via] == :sendmail
+ options[:via_options] ||= {}
+ options[:via_options][:location] ||= sendmail_binary
+ end
+
+ deliver build_mail(options)
+ end
+
+ private
+
+ def self.cross_reference_depricated_fields(options)
+ if options.has_key?(:smtp)
+ warn depricated_message(:smtp, :via_options)
+ options[:via_options] = options.delete(:smtp)
+ end
+
+ # cross-reference pony options to be compatible with keys mail expects
+ { :host => :address, :user => :user_name, :auth => :authentication, :tls => :enable_starttls_auto }.each do |key, val|
+ if options[:via_options] && options[:via_options].has_key?(key)
+ warn depricated_message(key, val)
+ options[:via_options][val] = options[:via_options].delete(key)
+ end
+ end
+
+ if options[:content_type] && options[:content_type] =~ /html/ && !options[:html_body]
+ warn depricated_message(:content_type, :html_body)
+ options[:html_body] = options[:body]
+ end
+
+ return options
+ end
+
+ def self.deliver(mail)
+ mail.deliver!
+ end
+
+ def self.default_delivery_method
+ File.executable?(sendmail_binary) ? :sendmail : :smtp
+ end
+
+ def self.build_mail(options)
+ mail = Mail.new do
+ to options[:to]
+ from options[:from] || 'pony@unknown'
+ cc options[:cc]
+ reply_to options[:reply_to]
+ bcc options[:bcc]
+ subject options[:subject]
+ date options[:date] || Time.now
+ message_id options[:message_id]
+ sender options[:sender] if options[:sender]
+
+ if options[:html_body]
+ html_part do
+ content_type 'text/html; charset=UTF-8'
+ body options[:html_body]
+ end
+ end
+
+ # If we're using attachments, the body needs to be a separate part. If not,
# we can just set the body directly.
- if options[:body] && (options[:html_body] || options[:attachments])
- text_part do
- body options[:body]
- end
- elsif options[:body]
- body options[:body]
- end
-
- delivery_method options[:via], (options.has_key?(:via_options) ? options[:via_options] : {})
+ if options[:body] && (options[:html_body] || options[:attachments])
+ text_part do
+ body options[:body]
+ end
+ elsif options[:body]
+ body options[:body]
+ end
+
+ delivery_method options[:via], (options.has_key?(:via_options) ? options[:via_options] : {})
end
- (options[:attachments] || []).each do |name, body|
- # mime-types wants to send these as "quoted-printable"
- if name =~ /\.xlsx$/
- mail.attachments[name] = {
- :content => Base64.encode64(body),
- :transfer_encoding => :base64
- }
- else
- mail.attachments[name] = body
- end
- end
-
- (options[:headers] ||= {}).each do |key, value|
- mail[key] = value
- end
-
- mail.charset = options[:charset] if options[:charset] # charset must be set after setting content_type
-
- mail
- end
-
- def self.sendmail_binary
- sendmail = `which sendmail`.chomp
- sendmail.empty? ? '/usr/sbin/sendmail' : sendmail
- end
-
- def self.via_possibilities
- %w(sendmail smtp).map{ |x| x.to_sym }
- end
-
- def self.depricated_message(method, alternative)
- warning_message = "warning: '#{method}' is deprecated"
- warning_message += "; use '#{alternative}' instead." if alternative
- return warning_message
- end
+ (options[:attachments] || []).each do |name, body|
+ # mime-types wants to send these as "quoted-printable"
+ if name =~ /\.xlsx$/
+ mail.attachments[name] = {
+ :content => Base64.encode64(body),
+ :transfer_encoding => :base64
+ }
+ else
+ mail.attachments[name] = body
+ end
+ end
+
+ (options[:headers] ||= {}).each do |key, value|
+ mail[key] = value
+ end
+
+ mail.charset = options[:charset] if options[:charset] # charset must be set after setting content_type
+
+ mail
+ end
+
+ def self.sendmail_binary
+ sendmail = `which sendmail`.chomp
+ sendmail.empty? ? '/usr/sbin/sendmail' : sendmail
+ end
+
+ def self.via_possibilities
+ %w(sendmail smtp).map{ |x| x.to_sym }
+ end
+
+ def self.depricated_message(method, alternative)
+ warning_message = "warning: '#{method}' is deprecated"
+ warning_message += "; use '#{alternative}' instead." if alternative
+ return warning_message
+ end
end
View
33 pony.gemspec
@@ -1,22 +1,23 @@
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
- s.name = %q{pony}
- s.version = "1.1"
+ s.name = %q{pony}
+ s.version = "1.1"
- s.description = "Send email in one command: Pony.mail(:to => 'someone@example.com', :body => 'hello')"
- s.summary = s.description
- s.authors = ["Adam Wiggins", "maint: Ben Prew"]
- s.email = %q{ben.prew@gmail.com}
- s.homepage = %q{http://github.com/benprew/pony}
- s.rubyforge_project = "pony"
+ s.description = "Send email in one command: Pony.mail(:to => 'someone@example.com', :body => 'hello')"
+ s.summary = s.description
+ s.authors = ["Adam Wiggins", "maint: Ben Prew"]
+ s.email = %q{ben.prew@gmail.com}
+ s.homepage = %q{http://github.com/benprew/pony}
+ s.rubyforge_project = "pony"
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
- s.files = ["README.rdoc", "Rakefile", "pony.gemspec" ] + Dir.glob("{lib,spec}/**/*")
- s.has_rdoc = false
- s.rdoc_options = ["--inline-source", "--charset=UTF-8"]
- s.require_paths = ["lib"]
- s.rubygems_version = %q{1.3.1}
- s.add_dependency( 'mail', '>2.0')
- s.platform = Gem::Platform::RUBY
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.files = ["README.rdoc", "Rakefile", "pony.gemspec" ] + Dir.glob("{lib,spec}/**/*")
+ s.has_rdoc = false
+ s.rdoc_options = ["--inline-source", "--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.1}
+ s.add_dependency 'mail', '>2.0'
+ s.add_development_dependency "rspec", ">= 2.0.0"
+ s.platform = Gem::Platform::RUBY
end
View
462 spec/pony_spec.rb
@@ -3,234 +3,238 @@
describe Pony do
- before(:each) do
- Pony.stub!(:deliver)
- end
-
- it "sends mail" do
- Pony.should_receive(:deliver) do |mail|
- mail.to.should == [ 'joe@example.com' ]
- mail.from.should == [ 'sender@example.com' ]
- mail.subject.should == 'hi'
- mail.body.should == 'Hello, Joe.'
- end
- Pony.mail(:to => 'joe@example.com', :from => 'sender@example.com', :subject => 'hi', :body => 'Hello, Joe.')
- end
-
- it "requires :to param" do
- lambda { Pony.mail({}) }.should raise_error(ArgumentError)
- end
-
- it "doesn't require any other param" do
- lambda { Pony.mail(:to => 'joe@example.com') }.should_not raise_error
- end
-
- it "should handle depricated options gracefully" do
- Pony.should_receive(:build_mail).with(hash_including(:via_options => {:address => 'test'}))
- Pony.mail(:to => 'foo@bar', :smtp => { :host => 'test' }, :via => :smtp)
- end
-
- it "should handle depricated content-type gracefully" do
- Pony.should_receive(:build_mail).with(hash_including(:html_body => "this is <h1>HTML</h1>"))
- Pony.mail(:to => 'foo@bar', :content_type => 'text/html', :body => 'this is <h1>HTML</h1>')
- end
- ####################
-
- describe "builds a Mail object with field:" do
- it "to" do
- Pony.build_mail(:to => 'joe@example.com').to.should == [ 'joe@example.com' ]
- end
-
- it "to with multiple recipients" do
- Pony.build_mail(:to => 'joe@example.com, friedrich@example.com').to.should == [ 'joe@example.com', 'friedrich@example.com' ]
- end
-
- it "to with multiple recipients and names" do
- Pony.build_mail(:to => 'joe@example.com, "Friedrich Hayek" <friedrich@example.com>').to.should == [ 'joe@example.com', 'friedrich@example.com' ]
- end
-
- it "to with multiple recipients and names in an array" do
- Pony.build_mail(:to => ['joe@example.com', '"Friedrich Hayek" <friedrich@example.com>']).to.should == [ 'joe@example.com', 'friedrich@example.com' ]
- end
-
- it "cc" do
- Pony.build_mail(:cc => 'joe@example.com').cc.should == [ 'joe@example.com' ]
- end
-
- it "cc with multiple recipients" do
- Pony.build_mail(:cc => 'joe@example.com, friedrich@example.com').cc.should == [ 'joe@example.com', 'friedrich@example.com' ]
- end
-
- it "from" do
- Pony.build_mail(:from => 'joe@example.com').from.should == [ 'joe@example.com' ]
- end
-
- it "bcc" do
- Pony.build_mail(:bcc => 'joe@example.com').bcc.should == [ 'joe@example.com' ]
- end
-
- it "bcc with multiple recipients" do
- Pony.build_mail(:bcc => 'joe@example.com, friedrich@example.com').bcc.should == [ 'joe@example.com', 'friedrich@example.com' ]
- end
-
- it "charset" do
- mail = Pony.build_mail(:charset => 'UTF-8')
- mail.charset.should == 'UTF-8'
- end
-
- it "default charset" do
- Pony.build_mail(:body => 'body').charset.should == 'UTF-8'
- Pony.build_mail(:body => 'body', :content_type => 'text/html').charset.should == 'UTF-8'
- end
-
- it "from (default)" do
- Pony.build_mail({}).from.should == [ 'pony@unknown' ]
- end
-
- it "subject" do
- Pony.build_mail(:subject => 'hello').subject.should == 'hello'
- end
-
- it "body" do
- Pony.build_mail(:body => 'What do you know, Joe?').body.should == 'What do you know, Joe?'
- end
-
- it "html_body" do
- Pony.build_mail(:html_body => 'What do you know, Joe?').parts.first.body.should == 'What do you know, Joe?'
- Pony.build_mail(:html_body => 'What do you know, Joe?').parts.first.content_type.should == 'text/html; charset=UTF-8'
- end
-
- it "date" do
- now = Time.now
- Pony.build_mail(:date => now).date.should == DateTime.parse(now.to_s)
- end
-
- it "content_type of html should set html_body" do
- Pony.should_receive(:build_mail).with(hash_including(:html_body => '<h1>test</h1>'))
- Pony.mail(:to => 'foo@bar', :content_type => 'text/html', :body => '<h1>test</h1>')
- end
-
- it "message_id" do
- Pony.build_mail(:message_id => '<abc@def.com>').message_id.should == 'abc@def.com'
- end
-
- it "custom headers" do
- Pony.build_mail(:headers => {"List-ID" => "<abc@def.com>"})['List-ID'].to_s.should == '<abc@def.com>'
- end
-
- it "sender" do
- Pony.build_mail(:sender => "abc@def.com")['Sender'].to_s.should == 'abc@def.com'
- end
-
- it "utf-8 encoded subject line" do
- mail = Pony.build_mail(:to => 'btp@foo', :subject => 'Café', :body => 'body body body')
- mail['subject'].encoded.should =~ /^Subject: =\?UTF-8/;
- end
-
- it "attachments" do
- mail = Pony.build_mail(:attachments => {"foo.txt" => "content of foo.txt"}, :body => 'test')
- mail.parts.length.should == 2
- mail.parts.first.to_s.should =~ /Content-Type: text\/plain/
- end
-
- it "suggests mime-type" do
- mail = Pony.build_mail(:attachments => {"foo.pdf" => "content of foo.pdf"})
- mail.parts.length.should == 1
- mail.parts.first.to_s.should =~ /Content-Type: application\/pdf/
- mail.parts.first.to_s.should =~ /filename=foo.pdf/
- mail.parts.first.content_transfer_encoding.to_s.should == 'base64'
- end
-
- it "encodes xlsx files as base64" do
- mail = Pony.build_mail(:attachments => {"foo.xlsx" => "content of foo.xlsx"})
- mail.parts.length.should == 1
- mail.parts.first.to_s.should =~ /Content-Type: application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet/
- mail.parts.first.to_s.should =~ /filename=foo.xlsx/
- mail.parts.first.content_transfer_encoding.to_s.should == 'base64'
- end
-
- it "passes cc and bcc as the list of recipients" do
- mail = Pony.build_mail(:to => ['to'], :cc => ['cc'], :from => ['from'], :bcc => ['bcc'])
- mail.destinations.should == ['to', 'cc', 'bcc']
- end
- end
-
- describe "transport" do
- it "transports via smtp if no sendmail binary" do
- Pony.stub!(:sendmail_binary).and_return('/does/not/exist')
- Pony.should_receive(:build_mail).with(hash_including(:via => :smtp))
- Pony.mail(:to => 'foo@bar')
- end
-
- it "defaults to sendmail if no via is specified and sendmail exists" do
- File.stub!(:executable?).and_return(true)
- Pony.should_receive(:build_mail).with(hash_including(:via => :sendmail))
- Pony.mail(:to => 'foo@bar')
- end
-
- describe "SMTP transport" do
-
- it "defaults to localhost as the SMTP server" do
- mail = Pony.build_mail(:to => "foo@bar", :enable_starttls_auto => true, :via => :smtp)
- mail.delivery_method.settings[:address].should == 'localhost'
- end
-
- it "enable starttls when tls option is true" do
- mail = Pony.build_mail(:to => "foo@bar", :enable_starttls_auto => true, :via => :smtp)
- mail.delivery_method.settings[:enable_starttls_auto].should == true
- end
- end
- end
-
- describe ":via option should over-ride the default transport mechanism" do
- it "should send via sendmail if :via => sendmail" do
- mail = Pony.build_mail(:to => 'joe@example.com', :via => :sendmail)
- mail.delivery_method.kind_of?(Mail::Sendmail).should == true
- end
-
- it "should send via smtp if :via => smtp" do
- mail = Pony.build_mail(:to => 'joe@example.com', :via => :smtp)
- mail.delivery_method.kind_of?(Mail::SMTP).should == true
- end
-
- it "should raise an error if via is neither smtp nor sendmail" do
- lambda { Pony.mail(:to => 'joe@plumber.com', :via => :pigeon) }.should raise_error(ArgumentError)
- end
- end
-
- describe "via_possibilities" do
- it "should contain smtp and sendmail" do
- Pony.via_possibilities.should == [:sendmail, :smtp]
- end
- end
-
-
- describe "sendmail binary location" do
- it "should default to /usr/sbin/sendmail if not in path" do
- Pony.stub!(:'`').and_return('')
- Pony.sendmail_binary.should == '/usr/sbin/sendmail'
- end
- end
-
- describe "default options" do
- it "should use default options" do
- Pony.should_receive(:build_mail).with(hash_including(:from => 'noreply@pony'))
- Pony.options = { :from => 'noreply@pony' }
- Pony.mail(:to => 'foo@bar')
- end
-
- it "should merge default options with options" do
- Pony.should_receive(:build_mail).with(hash_including(:from => 'override@pony'))
- Pony.options = { :from => 'noreply@pony' }
- Pony.mail(:from => 'override@pony', :to => "foo@bar")
- end
-
- it "should return the default options" do
- input = { :from => 'noreply@pony' }
- Pony.options = input
- output = Pony.options
- output.should == input
- end
- end
+ before(:each) do
+ Pony.stub!(:deliver)
+ end
+
+ it "sends mail" do
+ Pony.should_receive(:deliver) do |mail|
+ mail.to.should == [ 'joe@example.com' ]
+ mail.from.should == [ 'sender@example.com' ]
+ mail.subject.should == 'hi'
+ mail.body.should == 'Hello, Joe.'
+ end
+ Pony.mail(:to => 'joe@example.com', :from => 'sender@example.com', :subject => 'hi', :body => 'Hello, Joe.')
+ end
+
+ it "requires :to param" do
+ lambda { Pony.mail({}) }.should raise_error(ArgumentError)
+ end
+
+ it "doesn't require any other param" do
+ lambda { Pony.mail(:to => 'joe@example.com') }.should_not raise_error
+ end
+
+ it "should handle depricated options gracefully" do
+ Pony.should_receive(:build_mail).with(hash_including(:via_options => {:address => 'test'}))
+ Pony.mail(:to => 'foo@bar', :smtp => { :host => 'test' }, :via => :smtp)
+ end
+
+ it "should handle depricated content-type gracefully" do
+ Pony.should_receive(:build_mail).with(hash_including(:html_body => "this is <h1>HTML</h1>"))
+ Pony.mail(:to => 'foo@bar', :content_type => 'text/html', :body => 'this is <h1>HTML</h1>')
+ end
+ ####################
+
+ describe "builds a Mail object with field:" do
+ it "to" do
+ Pony.build_mail(:to => 'joe@example.com').to.should == [ 'joe@example.com' ]
+ end
+
+ it "to with multiple recipients" do
+ Pony.build_mail(:to => 'joe@example.com, friedrich@example.com').to.should == [ 'joe@example.com', 'friedrich@example.com' ]
+ end
+
+ it "to with multiple recipients and names" do
+ Pony.build_mail(:to => 'joe@example.com, "Friedrich Hayek" <friedrich@example.com>').to.should == [ 'joe@example.com', 'friedrich@example.com' ]
+ end
+
+ it "to with multiple recipients and names in an array" do
+ Pony.build_mail(:to => ['joe@example.com', '"Friedrich Hayek" <friedrich@example.com>']).to.should == [ 'joe@example.com', 'friedrich@example.com' ]
+ end
+
+ it "cc" do
+ Pony.build_mail(:cc => 'joe@example.com').cc.should == [ 'joe@example.com' ]
+ end
+
+ it "reply_to" do
+ Pony.build_mail(:reply_to => 'joe@example.com').reply_to.should == [ 'joe@example.com' ]
+ end
+
+ it "cc with multiple recipients" do
+ Pony.build_mail(:cc => 'joe@example.com, friedrich@example.com').cc.should == [ 'joe@example.com', 'friedrich@example.com' ]
+ end
+
+ it "from" do
+ Pony.build_mail(:from => 'joe@example.com').from.should == [ 'joe@example.com' ]
+ end
+
+ it "bcc" do
+ Pony.build_mail(:bcc => 'joe@example.com').bcc.should == [ 'joe@example.com' ]
+ end
+
+ it "bcc with multiple recipients" do
+ Pony.build_mail(:bcc => 'joe@example.com, friedrich@example.com').bcc.should == [ 'joe@example.com', 'friedrich@example.com' ]
+ end
+
+ it "charset" do
+ mail = Pony.build_mail(:charset => 'UTF-8')
+ mail.charset.should == 'UTF-8'
+ end
+
+ it "default charset" do
+ Pony.build_mail(:body => 'body').charset.should == 'UTF-8'
+ Pony.build_mail(:body => 'body', :content_type => 'text/html').charset.should == 'UTF-8'
+ end
+
+ it "from (default)" do
+ Pony.build_mail({}).from.should == [ 'pony@unknown' ]
+ end
+
+ it "subject" do
+ Pony.build_mail(:subject => 'hello').subject.should == 'hello'
+ end
+
+ it "body" do
+ Pony.build_mail(:body => 'What do you know, Joe?').body.should == 'What do you know, Joe?'
+ end
+
+ it "html_body" do
+ Pony.build_mail(:html_body => 'What do you know, Joe?').parts.first.body.should == 'What do you know, Joe?'
+ Pony.build_mail(:html_body => 'What do you know, Joe?').parts.first.content_type.should == 'text/html; charset=UTF-8'
+ end
+
+ it "date" do
+ now = Time.now
+ Pony.build_mail(:date => now).date.should == DateTime.parse(now.to_s)
+ end
+
+ it "content_type of html should set html_body" do
+ Pony.should_receive(:build_mail).with(hash_including(:html_body => '<h1>test</h1>'))
+ Pony.mail(:to => 'foo@bar', :content_type => 'text/html', :body => '<h1>test</h1>')
+ end
+
+ it "message_id" do
+ Pony.build_mail(:message_id => '<abc@def.com>').message_id.should == 'abc@def.com'
+ end
+
+ it "custom headers" do
+ Pony.build_mail(:headers => {"List-ID" => "<abc@def.com>"})['List-ID'].to_s.should == '<abc@def.com>'
+ end
+
+ it "sender" do
+ Pony.build_mail(:sender => "abc@def.com")['Sender'].to_s.should == 'abc@def.com'
+ end
+
+ it "utf-8 encoded subject line" do
+ mail = Pony.build_mail(:to => 'btp@foo', :subject => 'Café', :body => 'body body body')
+ mail['subject'].encoded.should =~ /^Subject: =\?UTF-8/;
+ end
+
+ it "attachments" do
+ mail = Pony.build_mail(:attachments => {"foo.txt" => "content of foo.txt"}, :body => 'test')
+ mail.parts.length.should == 2
+ mail.parts.first.to_s.should =~ /Content-Type: text\/plain/
+ end
+
+ it "suggests mime-type" do
+ mail = Pony.build_mail(:attachments => {"foo.pdf" => "content of foo.pdf"})
+ mail.parts.length.should == 1
+ mail.parts.first.to_s.should =~ /Content-Type: application\/pdf/
+ mail.parts.first.to_s.should =~ /filename=foo.pdf/
+ mail.parts.first.content_transfer_encoding.to_s.should == 'base64'
+ end
+
+ it "encodes xlsx files as base64" do
+ mail = Pony.build_mail(:attachments => {"foo.xlsx" => "content of foo.xlsx"})
+ mail.parts.length.should == 1
+ mail.parts.first.to_s.should =~ /Content-Type: application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet/
+ mail.parts.first.to_s.should =~ /filename=foo.xlsx/
+ mail.parts.first.content_transfer_encoding.to_s.should == 'base64'
+ end
+
+ it "passes cc and bcc as the list of recipients" do
+ mail = Pony.build_mail(:to => ['to'], :cc => ['cc'], :from => ['from'], :bcc => ['bcc'])
+ mail.destinations.should == ['to', 'cc', 'bcc']
+ end
+ end
+
+ describe "transport" do
+ it "transports via smtp if no sendmail binary" do
+ Pony.stub!(:sendmail_binary).and_return('/does/not/exist')
+ Pony.should_receive(:build_mail).with(hash_including(:via => :smtp))
+ Pony.mail(:to => 'foo@bar')
+ end
+
+ it "defaults to sendmail if no via is specified and sendmail exists" do
+ File.stub!(:executable?).and_return(true)
+ Pony.should_receive(:build_mail).with(hash_including(:via => :sendmail))
+ Pony.mail(:to => 'foo@bar')
+ end
+
+ describe "SMTP transport" do
+
+ it "defaults to localhost as the SMTP server" do
+ mail = Pony.build_mail(:to => "foo@bar", :enable_starttls_auto => true, :via => :smtp)
+ mail.delivery_method.settings[:address].should == 'localhost'
+ end
+
+ it "enable starttls when tls option is true" do
+ mail = Pony.build_mail(:to => "foo@bar", :enable_starttls_auto => true, :via => :smtp)
+ mail.delivery_method.settings[:enable_starttls_auto].should == true
+ end
+ end
+ end
+
+ describe ":via option should over-ride the default transport mechanism" do
+ it "should send via sendmail if :via => sendmail" do
+ mail = Pony.build_mail(:to => 'joe@example.com', :via => :sendmail)
+ mail.delivery_method.kind_of?(Mail::Sendmail).should == true
+ end
+
+ it "should send via smtp if :via => smtp" do
+ mail = Pony.build_mail(:to => 'joe@example.com', :via => :smtp)
+ mail.delivery_method.kind_of?(Mail::SMTP).should == true
+ end
+
+ it "should raise an error if via is neither smtp nor sendmail" do
+ lambda { Pony.mail(:to => 'joe@plumber.com', :via => :pigeon) }.should raise_error(ArgumentError)
+ end
+ end
+
+ describe "via_possibilities" do
+ it "should contain smtp and sendmail" do
+ Pony.via_possibilities.should == [:sendmail, :smtp]
+ end
+ end
+
+
+ describe "sendmail binary location" do
+ it "should default to /usr/sbin/sendmail if not in path" do
+ Pony.stub!(:'`').and_return('')
+ Pony.sendmail_binary.should == '/usr/sbin/sendmail'
+ end
+ end
+
+ describe "default options" do
+ it "should use default options" do
+ Pony.should_receive(:build_mail).with(hash_including(:from => 'noreply@pony'))
+ Pony.options = { :from => 'noreply@pony' }
+ Pony.mail(:to => 'foo@bar')
+ end
+
+ it "should merge default options with options" do
+ Pony.should_receive(:build_mail).with(hash_including(:from => 'override@pony'))
+ Pony.options = { :from => 'noreply@pony' }
+ Pony.mail(:from => 'override@pony', :to => "foo@bar")
+ end
+
+ it "should return the default options" do
+ input = { :from => 'noreply@pony' }
+ Pony.options = input
+ output = Pony.options
+ output.should == input
+ end
+ end
end

0 comments on commit bb4c152

Please sign in to comment.