Improving your Rails mailers with `email_address_with_name`

In almost all email programs, you can add a display name before your email address like so:

To: Matt Swanson <matt@example.com>

It’s a small touch, but it is a more human-readable way of addressing an email. Rails provides a helper utili…

In almost all email programs, you can add a display name before your email address like so:

To: Matt Swanson <matt@example.com>

It’s a small touch, but it is a more human-readable way of addressing an email. Rails provides a helper utility to format email addresses in this style without resorting to manual string manipulation.



Usage

Use email_address_with_name to add a name in-front on an email address in a standard way

ActionMailer::Base.email_address_with_name("swan3788@gmail.com", "Matt Swanson")
=> "Matt Swanson <swan3788@gmail.com>"

This helper is available in all Rails mailers.

class UserMailer < ApplicationMailer
  default from: 'notifications@example.com'

  def welcome_email
    @user = params[:user]

    mail(
      to: email_address_with_name(@user.email, @user.display_name),
      subject: 'You have a new message'
    )
  end
end



Options

This helper handles nil gracefully as well.

ActionMailer::Base.email_address_with_name("swan3788@gmail.com", nil)
=> "swan3788@gmail.com"

And it handles escaping characters automatically:

ActionMailer::Base.email_address_with_name("mike@example.com", "Michael J. Scott")
=> "\"Michael J. Scott\" <mike@example.com>"

ActionMailer::Base.email_address_with_name("chip@example.com", 'John "Chip" Smith')
=> "\"John \\\"Chip\\\" Smith\" <chip@example.com>"



Additional Resources

Rails API: ActionMailer::Base#email_address_with_name


Print Share Comment Cite Upload Translate
APA
matt swanson | Sciencx (2024-03-29T01:56:17+00:00) » Improving your Rails mailers with `email_address_with_name`. Retrieved from https://www.scien.cx/2021/05/03/improving-your-rails-mailers-with-email_address_with_name/.
MLA
" » Improving your Rails mailers with `email_address_with_name`." matt swanson | Sciencx - Monday May 3, 2021, https://www.scien.cx/2021/05/03/improving-your-rails-mailers-with-email_address_with_name/
HARVARD
matt swanson | Sciencx Monday May 3, 2021 » Improving your Rails mailers with `email_address_with_name`., viewed 2024-03-29T01:56:17+00:00,<https://www.scien.cx/2021/05/03/improving-your-rails-mailers-with-email_address_with_name/>
VANCOUVER
matt swanson | Sciencx - » Improving your Rails mailers with `email_address_with_name`. [Internet]. [Accessed 2024-03-29T01:56:17+00:00]. Available from: https://www.scien.cx/2021/05/03/improving-your-rails-mailers-with-email_address_with_name/
CHICAGO
" » Improving your Rails mailers with `email_address_with_name`." matt swanson | Sciencx - Accessed 2024-03-29T01:56:17+00:00. https://www.scien.cx/2021/05/03/improving-your-rails-mailers-with-email_address_with_name/
IEEE
" » Improving your Rails mailers with `email_address_with_name`." matt swanson | Sciencx [Online]. Available: https://www.scien.cx/2021/05/03/improving-your-rails-mailers-with-email_address_with_name/. [Accessed: 2024-03-29T01:56:17+00:00]
rf:citation
» Improving your Rails mailers with `email_address_with_name` | matt swanson | Sciencx | https://www.scien.cx/2021/05/03/improving-your-rails-mailers-with-email_address_with_name/ | 2024-03-29T01:56:17+00:00
https://github.com/addpipe/simple-recorderjs-demo