-
Notifications
You must be signed in to change notification settings - Fork 937
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose the SMTP envelope From and To addresses and allow them to be o…
…verridden. 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
- Loading branch information
Showing
12 changed files
with
252 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]>' | ||
# mail.smtp_envelope_from #=> '[email protected]' | ||
# | ||
# Also allows you to set the value by passing a value as a parameter | ||
# | ||
# Example: | ||
# | ||
# mail.smtp_envelope_from 'Mikel <[email protected]>' | ||
# mail.smtp_envelope_from #=> '[email protected]' | ||
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 <[email protected]>' | ||
# mail.smtp_envelope_from #=> '[email protected]' | ||
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 <[email protected]>' | ||
# mail.smtp_envelope_to #=> '[email protected]' | ||
# | ||
# Also allows you to set the value by passing a value as a parameter | ||
# | ||
# Example: | ||
# | ||
# mail.smtp_envelope_to ['Mikel <[email protected]>', 'Lindsaar <[email protected]>'] | ||
# mail.smtp_envelope_to #=> ['[email protected]', '[email protected]'] | ||
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 <[email protected]>' | ||
# mail.smtp_envelope_to #=> '[email protected]' | ||
# | ||
# mail.smtp_envelope_to = ['Mikel <[email protected]>', 'Lindsaar <[email protected]>'] | ||
# mail.smtp_envelope_to #=> ['[email protected]', '[email protected]'] | ||
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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ | |
Mail::Exim.should_receive(:call).with('/usr/sbin/exim', | ||
'-i -t -f "[email protected]" --', | ||
'"[email protected]" "[email protected]"', | ||
mail) | ||
mail.encoded) | ||
mail.deliver! | ||
end | ||
|
||
|
@@ -54,7 +54,7 @@ | |
Mail::Exim.should_receive(:call).with('/usr/sbin/exim', | ||
'-i -t -f "[email protected]" --', | ||
'"[email protected]"', | ||
mail) | ||
mail.encoded) | ||
|
||
mail.deliver | ||
|
||
|
@@ -77,7 +77,7 @@ | |
Mail::Exim.should_receive(:call).with('/usr/sbin/exim', | ||
'-i -t -f "[email protected]" --', | ||
'"[email protected]"', | ||
mail) | ||
mail.encoded) | ||
|
||
mail.deliver | ||
end | ||
|
@@ -98,7 +98,7 @@ | |
Mail::Exim.should_receive(:call).with('/usr/sbin/exim', | ||
'-i -t -f "[email protected]" --', | ||
'"[email protected]"', | ||
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" --', | ||
'"[email protected]"', | ||
mail) | ||
mail.encoded) | ||
mail.deliver | ||
end | ||
|
||
|
@@ -135,7 +135,7 @@ | |
Mail::Exim.should_receive(:call).with('/usr/sbin/exim', | ||
'-i -t -f "[email protected]" --', | ||
'"[email protected]"', | ||
mail) | ||
mail.encoded) | ||
mail.deliver | ||
end | ||
end | ||
|
@@ -152,9 +152,9 @@ | |
end | ||
|
||
Mail::Exim.should_receive(:call).with('/usr/sbin/exim', | ||
'-f "[email protected]" --', | ||
' -f "[email protected]" --', | ||
'"[email protected]" "[email protected]"', | ||
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\" --", | ||
'"[email protected]"', | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.