From 4875bc2ba1670289a34a155569af2f51abddc316 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 10 Dec 2012 23:04:19 -0700 Subject: [PATCH] Expose the SMTP envelope From and To addresses and allow them to be overridden. Envelope From address defaults to return_path || sender || from_addrs.first. Envelope To address defaults to destinations (to + cc + bcc). Updates all delivery methods to rely on the SMTP envelope. References #421 and rails/rails#5985 --- lib/mail/check_delivery_params.rb | 16 ++-- lib/mail/message.rb | 79 ++++++++++++++++ lib/mail/network/delivery_methods/sendmail.rb | 18 ++-- lib/mail/network/delivery_methods/smtp.rb | 10 +-- .../delivery_methods/smtp_connection.rb | 12 +-- spec/mail/message_spec.rb | 62 +++++++++++++ .../network/delivery_methods/exim_spec.rb | 24 ++--- .../delivery_methods/file_delivery_spec.rb | 4 +- .../network/delivery_methods/sendmail_spec.rb | 89 ++++++++++--------- .../delivery_methods/smtp_connection_spec.rb | 11 ++- .../network/delivery_methods/smtp_spec.rb | 51 ++++------- .../delivery_methods/test_mailer_spec.rb | 4 +- 12 files changed, 252 insertions(+), 128 deletions(-) diff --git a/lib/mail/check_delivery_params.rb b/lib/mail/check_delivery_params.rb index 0a287ca78..669ad4656 100644 --- a/lib/mail/check_delivery_params.rb +++ b/lib/mail/check_delivery_params.rb @@ -1,22 +1,20 @@ module Mail module CheckDeliveryParams def check_delivery_params(mail) - envelope_from = mail.return_path || mail.sender || mail.from_addrs.first - if envelope_from.blank? - raise ArgumentError.new('A sender (Return-Path, Sender or From) required to send a message') + if mail.smtp_envelope_from.blank? + raise ArgumentError.new('An SMTP From address is required to send a message. Set the message smtp_envelope_from, return_path, sender, or from address.') end - destinations ||= mail.destinations if mail.respond_to?(:destinations) && mail.destinations - if destinations.blank? - raise ArgumentError.new('At least one recipient (To, Cc or Bcc) is required to send a message') + if mail.smtp_envelope_to.blank? + raise ArgumentError.new('An SMTP To address is required to send a message. Set the message smtp_envelope_to, to, cc, or bcc address.') end - message ||= mail.encoded if mail.respond_to?(:encoded) + message = mail.encoded if mail.respond_to?(:encoded) if message.blank? - raise ArgumentError.new('A encoded content is required to send a message') + raise ArgumentError.new('An encoded message is required to send an email') end - [envelope_from, destinations, message] + [mail.smtp_envelope_from, mail.smtp_envelope_to, message] end end end diff --git a/lib/mail/message.rb b/lib/mail/message.rb index 6d1298297..d98eaec7d 100644 --- a/lib/mail/message.rb +++ b/lib/mail/message.rb @@ -108,6 +108,9 @@ def initialize(*args, &block) @charset = 'UTF-8' @defaulted_charset = true + @smtp_envelope_from = nil + @smtp_envelope_to = nil + @perform_deliveries = true @raise_delivery_errors = true @@ -1023,6 +1026,82 @@ def sender=( val ) header[:sender] = val end + # Returns the SMTP Envelope From value of the mail object, as a single + # string of an address spec. + # + # Defaults to Return-Path, Sender, or the first From address. + # + # Example: + # + # mail.smtp_envelope_from = 'Mikel ' + # mail.smtp_envelope_from #=> 'mikel@test.lindsaar.net' + # + # Also allows you to set the value by passing a value as a parameter + # + # Example: + # + # mail.smtp_envelope_from 'Mikel ' + # mail.smtp_envelope_from #=> 'mikel@test.lindsaar.net' + def smtp_envelope_from( val = nil ) + if val + self.smtp_envelope_from = val + else + @smtp_envelope_from || return_path || sender || from_addrs.first + end + end + + # Sets the From address on the SMTP Envelope. + # + # Example: + # + # mail.smtp_envelope_from = 'Mikel ' + # mail.smtp_envelope_from #=> 'mikel@test.lindsaar.net' + def smtp_envelope_from=( val ) + @smtp_envelope_from = val + end + + # Returns the SMTP Envelope To value of the mail object. + # + # Defaults to #destinations: To, Cc, and Bcc addresses. + # + # Example: + # + # mail.smtp_envelope_to = 'Mikel ' + # mail.smtp_envelope_to #=> 'mikel@test.lindsaar.net' + # + # Also allows you to set the value by passing a value as a parameter + # + # Example: + # + # mail.smtp_envelope_to ['Mikel ', 'Lindsaar '] + # mail.smtp_envelope_to #=> ['mikel@test.lindsaar.net', 'lindsaar@test.lindsaar.net'] + def smtp_envelope_to( val = nil ) + if val + self.smtp_envelope_to = val + else + @smtp_envelope_to || destinations + end + end + + # Sets the To addresses on the SMTP Envelope. + # + # Example: + # + # mail.smtp_envelope_to = 'Mikel ' + # mail.smtp_envelope_to #=> 'mikel@test.lindsaar.net' + # + # mail.smtp_envelope_to = ['Mikel ', 'Lindsaar '] + # mail.smtp_envelope_to #=> ['mikel@test.lindsaar.net', 'lindsaar@test.lindsaar.net'] + def smtp_envelope_to=( val ) + @smtp_envelope_to = + case val + when Array, NilClass + val + else + [val] + end + end + # Returns the decoded value of the subject field, as a single string. # # Example: diff --git a/lib/mail/network/delivery_methods/sendmail.rb b/lib/mail/network/delivery_methods/sendmail.rb index e87e66fa0..1491d0390 100644 --- a/lib/mail/network/delivery_methods/sendmail.rb +++ b/lib/mail/network/delivery_methods/sendmail.rb @@ -41,26 +41,24 @@ class Sendmail def initialize(values) self.settings = { :location => '/usr/sbin/sendmail', - :arguments => '-i -t' }.merge(values) + :arguments => '-i' }.merge(values) end attr_accessor :settings def deliver!(mail) - check_delivery_params(mail) + smtp_from, smtp_to, message = check_delivery_params(mail) - envelope_from = mail.return_path || mail.sender || mail.from_addrs.first - return_path = "-f #{self.class.shellquote(envelope_from)}" if envelope_from + from = "-f #{self.class.shellquote(smtp_from)}" + to = smtp_to.map { |to| self.class.shellquote(to) }.join(' ') - arguments = [settings[:arguments], return_path, '--'].compact.join(" ") - - quoted_destinations = mail.destinations.collect { |d| self.class.shellquote(d) } - self.class.call(settings[:location], arguments, quoted_destinations.join(' '), mail) + arguments = "#{settings[:arguments]} #{from} --" + self.class.call(settings[:location], arguments, to, message) end - def self.call(path, arguments, destinations, mail) + def self.call(path, arguments, destinations, encoded_message) popen "#{path} #{arguments} #{destinations}" do |io| - io.puts mail.encoded.to_lf + io.puts encoded_message.to_lf io.flush end end diff --git a/lib/mail/network/delivery_methods/smtp.rb b/lib/mail/network/delivery_methods/smtp.rb index a095385a0..6904ef38f 100644 --- a/lib/mail/network/delivery_methods/smtp.rb +++ b/lib/mail/network/delivery_methods/smtp.rb @@ -89,13 +89,13 @@ def initialize(values) :tls => nil }.merge!(values) end - + attr_accessor :settings - + # Send the message via SMTP. # The from and to attributes are optional. If not set, they are retrieve from the Message. def deliver!(mail) - envelope_from, destinations, message = check_delivery_params(mail) + smtp_from, smtp_to, message = check_delivery_params(mail) smtp = Net::SMTP.new(settings[:address], settings[:port]) if settings[:tls] || settings[:ssl] @@ -107,10 +107,10 @@ def deliver!(mail) smtp.enable_starttls_auto(ssl_context) end end - + response = nil smtp.start(settings[:domain], settings[:user_name], settings[:password], settings[:authentication]) do |smtp_obj| - response = smtp_obj.sendmail(message, envelope_from, destinations) + response = smtp_obj.sendmail(message, smtp_from, smtp_to) end if settings[:return_response] diff --git a/lib/mail/network/delivery_methods/smtp_connection.rb b/lib/mail/network/delivery_methods/smtp_connection.rb index b17e5c695..147335d8d 100644 --- a/lib/mail/network/delivery_methods/smtp_connection.rb +++ b/lib/mail/network/delivery_methods/smtp_connection.rb @@ -44,18 +44,18 @@ def initialize(values) self.smtp = values[:connection] self.settings = values end - + attr_accessor :smtp attr_accessor :settings - + # Send the message via SMTP. # The from and to attributes are optional. If not set, they are retrieve from the Message. def deliver!(mail) - envelope_from, destinations, message = check_delivery_params(mail) - response = smtp.sendmail(message, envelope_from, destinations) + smtp_from, smtp_to, message = check_delivery_params(mail) + response = smtp.sendmail(message, smtp_from, smtp_to) - settings[:return_response] ? response : self + settings[:return_response] ? response : self end - + end end diff --git a/spec/mail/message_spec.rb b/spec/mail/message_spec.rb index f66cf5a08..2b9c6e673 100644 --- a/spec/mail/message_spec.rb +++ b/spec/mail/message_spec.rb @@ -1731,4 +1731,66 @@ def self.delivering_email(mail) end + describe 'SMTP envelope From' do + it 'should respond' do + Mail::Message.new.should respond_to(:smtp_envelope_from) + end + + it 'should default to return_path, sender, or first from address' do + message = Mail::Message.new do + return_path 'return' + sender 'sender' + from 'from' + end + message.smtp_envelope_from.should eq 'return' + + message.return_path = nil + message.smtp_envelope_from.should eq 'sender' + + message.sender = nil + message.smtp_envelope_from.should eq 'from' + end + + it 'can be overridden' do + message = Mail::Message.new { return_path 'return' } + + message.smtp_envelope_from = 'envelope_from' + message.smtp_envelope_from.should eq 'envelope_from' + + message.smtp_envelope_from = 'declared_from' + message.smtp_envelope_from.should eq 'declared_from' + + message.smtp_envelope_from = nil + message.smtp_envelope_from.should eq 'return' + end + end + + describe 'SMTP envelope To' do + it 'should respond' do + Mail::Message.new.should respond_to(:smtp_envelope_to) + end + + it 'should default to destinations' do + message = Mail::Message.new do + to 'to' + cc 'cc' + bcc 'bcc' + end + message.smtp_envelope_to.should eq message.destinations + end + + it 'can be overridden' do + message = Mail::Message.new { to 'to' } + + message.smtp_envelope_to = 'envelope_to' + message.smtp_envelope_to.should eq %w(envelope_to) + + message.smtp_envelope_to = 'declared_to' + message.smtp_envelope_to.should eq %w(declared_to) + + message.smtp_envelope_to = nil + message.smtp_envelope_to.should eq %w(to) + end + end + end diff --git a/spec/mail/network/delivery_methods/exim_spec.rb b/spec/mail/network/delivery_methods/exim_spec.rb index d91ef1751..3b08064c3 100644 --- a/spec/mail/network/delivery_methods/exim_spec.rb +++ b/spec/mail/network/delivery_methods/exim_spec.rb @@ -30,7 +30,7 @@ Mail::Exim.should_receive(:call).with('/usr/sbin/exim', '-i -t -f "roger@test.lindsaar.net" --', '"marcel@test.lindsaar.net" "bob@test.lindsaar.net"', - mail) + mail.encoded) mail.deliver! end @@ -54,7 +54,7 @@ Mail::Exim.should_receive(:call).with('/usr/sbin/exim', '-i -t -f "return@test.lindsaar.net" --', '"to@test.lindsaar.net"', - mail) + mail.encoded) mail.deliver @@ -77,7 +77,7 @@ Mail::Exim.should_receive(:call).with('/usr/sbin/exim', '-i -t -f "sender@test.lindsaar.net" --', '"to@test.lindsaar.net"', - mail) + mail.encoded) mail.deliver end @@ -98,7 +98,7 @@ Mail::Exim.should_receive(:call).with('/usr/sbin/exim', '-i -t -f "from@test.lindsaar.net" --', '"to@test.lindsaar.net"', - mail) + mail.encoded) mail.deliver end @@ -118,7 +118,7 @@ Mail::Exim.should_receive(:call).with('/usr/sbin/exim', '-i -t -f "\"from+suffix test\"@test.lindsaar.net" --', '"to@test.lindsaar.net"', - mail) + mail.encoded) mail.deliver end @@ -135,7 +135,7 @@ Mail::Exim.should_receive(:call).with('/usr/sbin/exim', '-i -t -f "from@test.lindsaar.net" --', '"-hyphen@test.lindsaar.net"', - mail) + mail.encoded) mail.deliver end end @@ -152,9 +152,9 @@ end Mail::Exim.should_receive(:call).with('/usr/sbin/exim', - '-f "from@test.lindsaar.net" --', + ' -f "from@test.lindsaar.net" --', '"marcel@test.lindsaar.net" "bob@test.lindsaar.net"', - mail) + mail.encoded) mail.deliver! end @@ -170,9 +170,9 @@ end Mail::Exim.should_receive(:call).with('/usr/sbin/exim', - "-f \"\\\"foo\\\\\\\"\\;touch /tmp/PWNED\\;\\\\\\\"\\\"@blah.com\" --", + " -f \"\\\"foo\\\\\\\"\\;touch /tmp/PWNED\\;\\\\\\\"\\\"@blah.com\" --", '"marcel@test.lindsaar.net"', - mail) + mail.encoded) mail.deliver! end @@ -186,7 +186,7 @@ subject "Email with no sender" body "body" end - end.should raise_error('A sender (Return-Path, Sender or From) required to send a message') + end.should raise_error('An SMTP From address is required to send a message. Set the message smtp_envelope_from, return_path, sender, or from address.') end it "should raise an error if no recipient if defined" do @@ -199,6 +199,6 @@ subject "Email with no recipient" body "body" end - end.should raise_error('At least one recipient (To, Cc or Bcc) is required to send a message') + end.should raise_error('An SMTP To address is required to send a message. Set the message smtp_envelope_to, to, cc, or bcc address.') end end diff --git a/spec/mail/network/delivery_methods/file_delivery_spec.rb b/spec/mail/network/delivery_methods/file_delivery_spec.rb index 62b19da87..ed93162dd 100644 --- a/spec/mail/network/delivery_methods/file_delivery_spec.rb +++ b/spec/mail/network/delivery_methods/file_delivery_spec.rb @@ -100,7 +100,7 @@ subject "Email with no sender" body "body" end - end.should raise_error('A sender (Return-Path, Sender or From) required to send a message') + end.should raise_error('An SMTP From address is required to send a message. Set the message smtp_envelope_from, return_path, sender, or from address.') end it "should raise an error if no recipient if defined" do @@ -114,7 +114,7 @@ subject "Email with no recipient" body "body" end - end.should raise_error('At least one recipient (To, Cc or Bcc) is required to send a message') + end.should raise_error('An SMTP To address is required to send a message. Set the message smtp_envelope_to, to, cc, or bcc address.') end end diff --git a/spec/mail/network/delivery_methods/sendmail_spec.rb b/spec/mail/network/delivery_methods/sendmail_spec.rb index 55b30198f..46cfec971 100644 --- a/spec/mail/network/delivery_methods/sendmail_spec.rb +++ b/spec/mail/network/delivery_methods/sendmail_spec.rb @@ -28,9 +28,9 @@ end Mail::Sendmail.should_receive(:call).with('/usr/sbin/sendmail', - '-i -t -f "roger@test.lindsaar.net" --', + '-i -f "roger@test.lindsaar.net" --', '"marcel@test.lindsaar.net" "bob@test.lindsaar.net"', - mail) + mail.encoded) mail.deliver! end @@ -45,14 +45,14 @@ subject 'invalid RFC2822' end - Mail::Sendmail.should_receive(:popen).with('/usr/sbin/sendmail -i -t -f "roger@test.lindsaar.net" -- "marcel@test.lindsaar.net" "bob@test.lindsaar.net"') + Mail::Sendmail.should_receive(:popen).with('/usr/sbin/sendmail -i -f "roger@test.lindsaar.net" -- "marcel@test.lindsaar.net" "bob@test.lindsaar.net"') mail.deliver! end - describe "return path" do + describe 'SMTP From' do - it "should send an email with a return-path using sendmail" do + it 'should explicitly pass an envelope From address to sendmail' do Mail.defaults do delivery_method :sendmail end @@ -60,45 +60,46 @@ mail = Mail.new do to "to@test.lindsaar.net" from "from@test.lindsaar.net" - sender "sender@test.lindsaar.net" - subject "Can't set the return-path" - return_path "return@test.lindsaar.net" + subject 'Can\'t set the return-path' message_id "<1234@test.lindsaar.net>" body "body" + + smtp_envelope_from 'smtp_from@test.lindsaar.net' end - + Mail::Sendmail.should_receive(:call).with('/usr/sbin/sendmail', - '-i -t -f "return@test.lindsaar.net" --', + '-i -f "smtp_from@test.lindsaar.net" --', '"to@test.lindsaar.net"', - mail) - + mail.encoded) + mail.deliver end - it "should use the sender address is no return path is specified" do + it "should escape the From address" do Mail.defaults do delivery_method :sendmail end mail = Mail.new do - to "to@test.lindsaar.net" - from "from@test.lindsaar.net" - sender "sender@test.lindsaar.net" - subject "Can't set the return-path" - message_id "<1234@test.lindsaar.net>" - body "body" + to 'to@test.lindsaar.net' + from '"from+suffix test"@test.lindsaar.net' + subject 'Can\'t set the return-path' + message_id '<1234@test.lindsaar.net>' + body 'body' end Mail::Sendmail.should_receive(:call).with('/usr/sbin/sendmail', - '-i -t -f "sender@test.lindsaar.net" --', - '"to@test.lindsaar.net"', - mail) - + '-i -f "\"from+suffix test\"@test.lindsaar.net" --', + '"to@test.lindsaar.net"', + mail.encoded) mail.deliver end - - it "should use the from address is no return path or sender are specified" do + end + + describe 'SMTP To' do + + it 'should explicitly pass envelope To addresses to sendmail' do Mail.defaults do delivery_method :sendmail end @@ -106,35 +107,37 @@ mail = Mail.new do to "to@test.lindsaar.net" from "from@test.lindsaar.net" - subject "Can't set the return-path" + subject 'Can\'t set the return-path' message_id "<1234@test.lindsaar.net>" body "body" + + smtp_envelope_to 'smtp_to@test.lindsaar.net' end Mail::Sendmail.should_receive(:call).with('/usr/sbin/sendmail', - '-i -t -f "from@test.lindsaar.net" --', - '"to@test.lindsaar.net"', - mail) + '-i -f "from@test.lindsaar.net" --', + '"smtp_to@test.lindsaar.net"', + mail.encoded) mail.deliver end - it "should escape the return path address" do + it "should escape the To address" do Mail.defaults do delivery_method :sendmail end mail = Mail.new do - to 'to@test.lindsaar.net' - from '"from+suffix test"@test.lindsaar.net' + to '"to+suffix test"@test.lindsaar.net' + from 'from@test.lindsaar.net' subject 'Can\'t set the return-path' message_id '<1234@test.lindsaar.net>' body 'body' end Mail::Sendmail.should_receive(:call).with('/usr/sbin/sendmail', - '-i -t -f "\"from+suffix test\"@test.lindsaar.net" --', - '"to@test.lindsaar.net"', - mail) + '-i -f "from@test.lindsaar.net" --', + '"\"to+suffix test\"@test.lindsaar.net"', + mail.encoded) mail.deliver end @@ -149,9 +152,9 @@ end Mail::Sendmail.should_receive(:call).with('/usr/sbin/sendmail', - '-i -t -f "from@test.lindsaar.net" --', + '-i -f "from@test.lindsaar.net" --', '"-hyphen@test.lindsaar.net"', - mail) + mail.encoded) mail.deliver end end @@ -168,9 +171,9 @@ end Mail::Sendmail.should_receive(:call).with('/usr/sbin/sendmail', - '-f "from@test.lindsaar.net" --', + ' -f "from@test.lindsaar.net" --', '"marcel@test.lindsaar.net" "bob@test.lindsaar.net"', - mail) + mail.encoded) mail.deliver! end @@ -186,9 +189,9 @@ end Mail::Sendmail.should_receive(:call).with('/usr/sbin/sendmail', - "-f \"\\\"foo\\\\\\\"\\;touch /tmp/PWNED\\;\\\\\\\"\\\"@blah.com\" --", + " -f \"\\\"foo\\\\\\\"\\;touch /tmp/PWNED\\;\\\\\\\"\\\"@blah.com\" --", %("\\\"foo\\\\\\\"\\;touch /tmp/PWNED\\;\\\\\\\"\\\"@blah.com"), - mail) + mail.encoded) mail.deliver! end @@ -202,7 +205,7 @@ subject "Email with no sender" body "body" end - end.should raise_error('A sender (Return-Path, Sender or From) required to send a message') + end.should raise_error('An SMTP From address is required to send a message. Set the message smtp_envelope_from, return_path, sender, or from address.') end it "should raise an error if no recipient if defined" do @@ -215,6 +218,6 @@ subject "Email with no recipient" body "body" end - end.should raise_error('At least one recipient (To, Cc or Bcc) is required to send a message') + end.should raise_error('An SMTP To address is required to send a message. Set the message smtp_envelope_to, to, cc, or bcc address.') end end diff --git a/spec/mail/network/delivery_methods/smtp_connection_spec.rb b/spec/mail/network/delivery_methods/smtp_connection_spec.rb index b517831a7..84a245e10 100644 --- a/spec/mail/network/delivery_methods/smtp_connection_spec.rb +++ b/spec/mail/network/delivery_methods/smtp_connection_spec.rb @@ -19,11 +19,14 @@ from 'roger@test.lindsaar.net' to 'marcel@test.lindsaar.net, bob@test.lindsaar.net' subject 'invalid RFC2822' + + smtp_envelope_from 'smtp_from' + smtp_envelope_to 'smtp_to' end MockSMTP.deliveries[0][0].should eq mail.encoded - MockSMTP.deliveries[0][1].should eq mail.from[0] - MockSMTP.deliveries[0][2].should eq mail.destinations + MockSMTP.deliveries[0][1].should eq 'smtp_from' + MockSMTP.deliveries[0][2].should eq %w(smtp_to) end it "should be able to return actual SMTP protocol response" do @@ -56,7 +59,7 @@ subject "Email with no sender" body "body" end - end.should raise_error('A sender (Return-Path, Sender or From) required to send a message') + end.should raise_error('An SMTP From address is required to send a message. Set the message smtp_envelope_from, return_path, sender, or from address.') end it "should raise an error if no recipient if defined" do @@ -71,6 +74,6 @@ subject "Email with no recipient" body "body" end - end.should raise_error('At least one recipient (To, Cc or Bcc) is required to send a message') + end.should raise_error('An SMTP To address is required to send a message. Set the message smtp_envelope_to, to, cc, or bcc address.') end end diff --git a/spec/mail/network/delivery_methods/smtp_spec.rb b/spec/mail/network/delivery_methods/smtp_spec.rb index 9e56191bd..307c32875 100644 --- a/spec/mail/network/delivery_methods/smtp_spec.rb +++ b/spec/mail/network/delivery_methods/smtp_spec.rb @@ -29,11 +29,14 @@ from 'roger@moore.com' to 'marcel@amont.com' subject 'invalid RFC2822' + + smtp_envelope_from 'smtp_from' + smtp_envelope_to 'smtp_to' end MockSMTP.deliveries[0][0].should eq mail.encoded - MockSMTP.deliveries[0][1].should eq mail.from[0] - MockSMTP.deliveries[0][2].should eq mail.destinations + MockSMTP.deliveries[0][1].should eq 'smtp_from' + MockSMTP.deliveries[0][2].should eq %w(smtp_to) end it "should be able to send itself" do @@ -149,53 +152,31 @@ def redefine_verify_none(new_value) doing { mail.deliver! }.should_not raise_error(TypeError) end end - - describe "return path" do - it "should use the return path if specified" do - Mail.deliver do - to "to@someemail.com" - from "from@someemail.com" - sender "sender@test.lindsaar.net" - subject "Can't set the return-path" - return_path "bounce@someemail.com" - message_id "<1234@someemail.com>" - body "body" - end - MockSMTP.deliveries[0][1].should eq "bounce@someemail.com" - end + describe "SMTP Envelope" do - it "should use the sender address is no return path is specified" do + it "uses the envelope From and To addresses" do Mail.deliver do to "to@someemail.com" from "from@someemail.com" - sender "sender@test.lindsaar.net" - subject "Can't set the return-path" - message_id "<1234@someemail.com>" - body "body" - end - MockSMTP.deliveries[0][1].should eq "sender@test.lindsaar.net" - end - - it "should use the from address is no return path or sender is specified" do - Mail.deliver do - to "to@someemail.com" - from "from@someemail.com" - subject "Can't set the return-path" message_id "<1234@someemail.com>" body "body" + + smtp_envelope_to "smtp_to@someemail.com" + smtp_envelope_from "smtp_from@someemail.com" end - MockSMTP.deliveries[0][1].should eq "from@someemail.com" + MockSMTP.deliveries[0][1].should eq 'smtp_from@someemail.com' + MockSMTP.deliveries[0][2].should eq %w(smtp_to@someemail.com) end - it "should raise an error if no sender is defined" do + it "should raise if there is no envelope From address" do lambda do Mail.deliver do to "to@somemail.com" subject "Email with no sender" body "body" end - end.should raise_error('A sender (Return-Path, Sender or From) required to send a message') + end.should raise_error('An SMTP From address is required to send a message. Set the message smtp_envelope_from, return_path, sender, or from address.') end it "should raise an error if no recipient if defined" do @@ -205,8 +186,8 @@ def redefine_verify_none(new_value) subject "Email with no recipient" body "body" end - end.should raise_error('At least one recipient (To, Cc or Bcc) is required to send a message') + end.should raise_error('An SMTP To address is required to send a message. Set the message smtp_envelope_to, to, cc, or bcc address.') end end - + end diff --git a/spec/mail/network/delivery_methods/test_mailer_spec.rb b/spec/mail/network/delivery_methods/test_mailer_spec.rb index 39c54cd98..18feabcc8 100644 --- a/spec/mail/network/delivery_methods/test_mailer_spec.rb +++ b/spec/mail/network/delivery_methods/test_mailer_spec.rb @@ -64,7 +64,7 @@ subject "Email with no sender" body "body" end - end.should raise_error('A sender (Return-Path, Sender or From) required to send a message') + end.should raise_error('An SMTP From address is required to send a message. Set the message smtp_envelope_from, return_path, sender, or from address.') end it "should raise an error if no recipient if defined" do @@ -77,7 +77,7 @@ subject "Email with no recipient" body "body" end - end.should raise_error('At least one recipient (To, Cc or Bcc) is required to send a message') + end.should raise_error('An SMTP To address is required to send a message. Set the message smtp_envelope_to, to, cc, or bcc address.') end end