Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
43 lines (31 sloc) 1.68 KB
layout title categories published meta
post
Building internet safe addresses for ActionMailer using Mail::Address
– blog
true
description index
Sometimes you need to pass in some funky characters to your mail parameters, use Mail::Address to do it safely.
true

Recently I needed to send emails on behalf of my users, using their full name and email.

I could just pass their unsanitized input into a string like Full Name <name@example.com>, then hand that off to ActionMailer. However, I had a bunch of users with names that included characters such as @ and <, so I went searching for a safer solution.

What I discovered was that Mail (which ships with Rails and does the actual sending of emails) has a super handy Mail::Address class, which can build internet safe email addresses.

Example Usage

To build an internet safe address for ActionMailer, you can use the tap method like so:

Mail::Address.new.tap do |m|
  m.address = 'name@example.com'
  m.display_name = 'Super <@>, Name'
end.to_s

# Outputs: "\"Super <@>, Name\" <name@example.com>"

How to use with ActionMailer

To use this address in the "from" field in an email, just pass the output of to_s to the from argument when sending an email. Like this:

class GuestMailer < ApplicationMailer
  def get_hyped
    user_address = Mail::Address.new.tap do |m|
      m.address = 'name@example.com'
      m.display_name = 'Super <@>, Name'
    end.to_s

    mail to: 'guest@example.com', from: user_address
  end
end
You can’t perform that action at this time.