Skip to content

Commit 524c4ca

Browse files
committed
Return just the address if name is blank
Otherwise the returned string would look like " <[email protected]>".
1 parent ceb4b94 commit 524c4ca

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

actionmailer/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* `email_address_with_name` returns just the address if name is blank.
2+
3+
*Thomas Hutterer*
4+
5+
16
## Rails 7.0.0.alpha2 (September 15, 2021) ##
27

38
* No changes.

actionmailer/lib/action_mailer/base.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,10 +563,12 @@ def deliver_mail(mail) # :nodoc:
563563
end
564564

565565
# Returns an email in the format "Name <[email protected]>".
566+
#
567+
# If the name is a blank string, it returns just the address.
566568
def email_address_with_name(address, name)
567569
Mail::Address.new.tap do |builder|
568570
builder.address = address
569-
builder.display_name = name
571+
builder.display_name = name.presence
570572
end.to_s
571573
end
572574

@@ -638,6 +640,8 @@ def mailer_name
638640
end
639641

640642
# Returns an email in the format "Name <[email protected]>".
643+
#
644+
# If the name is a blank string, it returns just the address.
641645
def email_address_with_name(address, name)
642646
self.class.email_address_with_name(address, name)
643647
end

actionmailer/test/base_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ class BaseTest < ActiveSupport::TestCase
8989
assert_equal("Mikel <[email protected]>", email["Reply-To"].value)
9090
end
9191

92+
test "mail() using email_address_with_name with blank string as name" do
93+
email = BaseMailer.with_blank_name
94+
assert_equal("[email protected]", email["To"].value)
95+
end
96+
9297
# Custom headers
9398
test "custom headers" do
9499
email = BaseMailer.welcome

actionmailer/test/mailers/base_mailer.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ def with_name
3131
mail(template_name: "welcome", to: to)
3232
end
3333

34+
def with_blank_name
35+
to = email_address_with_name("[email protected]", "")
36+
mail(template_name: "welcome", to: to)
37+
end
38+
3439
def html_only(hash = {})
3540
mail(hash)
3641
end

guides/source/action_mailer_basics.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ def welcome_email
390390
end
391391
```
392392

393+
If the name is a blank string, it returns just the address.
394+
393395
[`email_address_with_name`]: https://api.rubyonrails.org/classes/ActionMailer/Base.html#method-i-email_address_with_name
394396

395397
### Mailer Views

0 commit comments

Comments
 (0)