Skip to content

Commit

Permalink
Merge pull request #48782 from c960657/preview-smtp-envelope
Browse files Browse the repository at this point in the history
Only show SMTP envelope recipient when relevant
  • Loading branch information
jonathanhefner committed Nov 7, 2023
2 parents 2f34889 + e5481ed commit 0157819
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
4 changes: 4 additions & 0 deletions railties/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* In mailer preview, only show SMTP-To if it differs from the union of To, Cc and Bcc.

*Christian Schmidt*

* Enable YJIT by default on new applications running Ruby 3.3+

Adds a `config/initializers/enable_yjit.rb` initializer that enables YJIT
Expand Down
6 changes: 3 additions & 3 deletions railties/lib/rails/templates/rails/mailers/email.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@
<body>
<header>
<dl>
<% if @email.respond_to?(:smtp_envelope_from) && Array(@email.from) != Array(@email.smtp_envelope_from) %>
<% if Array(@email.from) != Array(@email.smtp_envelope_from) %>
<dt>SMTP-From:</dt>
<dd id="smtp_from"><%= @email.smtp_envelope_from %></dd>
<% end %>
<% if @email.respond_to?(:smtp_envelope_to) && @email.to != @email.smtp_envelope_to %>
<% if Set[*@email.to, *@email.cc, *@email.bcc] != Set[*@email.smtp_envelope_to] %>
<dt>SMTP-To:</dt>
<dd id="smtp_to"><%= @email.smtp_envelope_to %></dd>
<dd id="smtp_to"><%= @email.smtp_envelope_to.join(", ") %></dd>
<% end %>

<dt>From:</dt>
Expand Down
44 changes: 41 additions & 3 deletions railties/test/application/mailer_previews_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,11 @@ def foo

get "/rails/mailers/notifier/foo"
assert_equal 200, last_response.status
assert_match "Ruby on Rails &lt;core@rubyonrails.org&gt;", last_response.body
assert_match "Andrew White &lt;andyw@pixeltrix.co.uk&gt;", last_response.body
assert_match "David Heinemeier Hansson &lt;david@heinemeierhansson.com&gt;", last_response.body
assert_match '<dd id="from">Ruby on Rails &lt;core@rubyonrails.org&gt;</dd>', last_response.body
assert_match '<dd id="to">Andrew White &lt;andyw@pixeltrix.co.uk&gt;</dd>', last_response.body
assert_match '<dd id="cc">David Heinemeier Hansson &lt;david@heinemeierhansson.com&gt;</dd>', last_response.body
assert_no_match '<dd id="smtp_from">', last_response.body
assert_no_match '<dd id="smtp_to">', last_response.body

get "/rails/mailers/download/notifier/foo"
email = Mail.read_from_string(last_response.body)
Expand All @@ -521,6 +523,42 @@ def foo
assert_equal ["david@heinemeierhansson.com"], email.cc
end

test "message header shows SMTP envelope To and From when different than message headers" do
mailer "notifier", <<-RUBY
class Notifier < ActionMailer::Base
default from: "from@example.com"
def foo
message.smtp_envelope_from = "smtp-from@example.com"
message.smtp_envelope_to = ["to@example.com", "bcc@example.com"]
mail to: "to@example.com"
end
end
RUBY

mailer_preview "notifier", <<-RUBY
class NotifierPreview < ActionMailer::Preview
def foo
Notifier.foo
end
end
RUBY

text_template "notifier/foo", <<-RUBY
Hello, World!
RUBY

app("development")

get "/rails/mailers/notifier/foo"
assert_equal 200, last_response.status
assert_match '<dd id="from">from@example.com</dd>', last_response.body
assert_match '<dd id="smtp_from">smtp-from@example.com</dd>', last_response.body
assert_match '<dd id="to">to@example.com</dd>', last_response.body
assert_match '<dd id="smtp_to">to@example.com, bcc@example.com</dd>', last_response.body
end

test "part menu selects correct option" do
mailer "notifier", <<-RUBY
class Notifier < ActionMailer::Base
Expand Down

0 comments on commit 0157819

Please sign in to comment.