---
title: "Rails 6 adds ActionMailer#email_address_with_name"
description:
  "Rails 6 adds email_address_with_name to ActionMailer to properly escape email
  addresses with names."
canonical_url: "https://www.bigbinary.com/blog/rails-6-adds-actionmailer-email_address_with_name"
markdown_url: "https://www.bigbinary.com/blog/rails-6-adds-actionmailer-email_address_with_name.md"
---

# Rails 6 adds ActionMailer#email_address_with_name

Rails 6 adds email_address_with_name to ActionMailer to properly escape email
addresses with names.

- Author: Taha Husain
- Published: October 22, 2019
- Categories: Rails 6, Rails

When using `ActionMailer::Base#mail`, if we want to display name and email
address of the user in email, we can pass a string in format
`"John Smith" <john@example.com>` in `to`, `from` or `reply_to` options.

Before Rails 6, we had to join name and email address using string interpolation
as mentioned in
[Rails 5.2 Guides](https://guides.rubyonrails.org/v5.2/action_mailer_basics.html#sending-email-with-name)
and shown below.

```ruby
  email_with_name = %("John Smith" <john@example.com>)
  mail(
    to: email_with_name,
    subject: 'Hey Rails 5.2!'
  )
```

Problem with string interpolation is it doesn't escape unexpected special
characters like quotes(") in the name.

Here's an example.

### Rails 5.2

```ruby

irb(main):001:0> %("John P Smith" <john@example.com>)
=> "\"John P Smith\" <john@example.com>"

irb(main):002:0> %('John "P" Smith' <john@example.com>)
=> "'John \"P\" Smith' <john@example.com>"

```

Rails 6 adds
[`ActionMailer::Base#email_address_with_name`](https://github.com/rails/rails/pull/36454)
to join name and email address in the format `"John Smith" <john@example.com>`
and take care of escaping special characters.

### Rails 6.1.0.alpha

```ruby

irb(main):001:0> ActionMailer::Base.email_address_with_name("john@example.com", "John P Smith")
=> "John P Smith <john@example.com>"

irb(main):002:0> ActionMailer::Base.email_address_with_name("john@example.com", 'John "P" Smith')
=> "\"John \\\"P\\\" Smith\" <john@example.com>"

```

```ruby
mail(
to: email_address_with_name("john@example.com", "John Smith"),
subject: 'Hey Rails 6!'
)
```

Here's the relevant [pull request](https://github.com/rails/rails/pull/36454)
for this change.

## Links

- [Human page](https://www.bigbinary.com/blog/rails-6-adds-actionmailer-email_address_with_name)
