Merge pull request #42970 from thutterer/email_address_with_blank_name

Return just the address if name is blank
This commit is contained in:
Rafael França 2021-09-21 18:41:19 -04:00 committed by GitHub
commit 02537722f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 1 deletions

@ -1,3 +1,8 @@
* `email_address_with_name` returns just the address if name is blank.
*Thomas Hutterer*
## Rails 7.0.0.alpha2 (September 15, 2021) ##
* No changes.

@ -563,10 +563,12 @@ def deliver_mail(mail) # :nodoc:
end
# Returns an email in the format "Name <email@example.com>".
#
# If the name is a blank string, it returns just the address.
def email_address_with_name(address, name)
Mail::Address.new.tap do |builder|
builder.address = address
builder.display_name = name
builder.display_name = name.presence
end.to_s
end
@ -638,6 +640,8 @@ def mailer_name
end
# Returns an email in the format "Name <email@example.com>".
#
# If the name is a blank string, it returns just the address.
def email_address_with_name(address, name)
self.class.email_address_with_name(address, name)
end

@ -89,6 +89,11 @@ class BaseTest < ActiveSupport::TestCase
assert_equal("Mikel <mikel@test.lindsaar.net>", email["Reply-To"].value)
end
test "mail() using email_address_with_name with blank string as name" do
email = BaseMailer.with_blank_name
assert_equal("sunny@example.com", email["To"].value)
end
# Custom headers
test "custom headers" do
email = BaseMailer.welcome

@ -31,6 +31,11 @@ def with_name
mail(template_name: "welcome", to: to)
end
def with_blank_name
to = email_address_with_name("sunny@example.com", "")
mail(template_name: "welcome", to: to)
end
def html_only(hash = {})
mail(hash)
end

@ -390,6 +390,8 @@ def welcome_email
end
```
If the name is a blank string, it returns just the address.
[`email_address_with_name`]: https://api.rubyonrails.org/classes/ActionMailer/Base.html#method-i-email_address_with_name
### Mailer Views