Delivering Email On Behalf Of Users

A recipe for a better user experience in emails sent between users via my Rails app.


When I receive an email from an automated system like a Rails app, it can be disorienting if the sender shows up in the email program as “admin” or “donotreply”.

What I want is something like this:



Install email-spec

I’m a fan of Ben Mabey’s email-spec gem, so I’ll install that:

group :test do
  gem 'email_spec'

I create a features/support/email.rb file:

require 'email_spec' # add this line if you use spork
require 'email_spec/cucumber'

Then generate some step definitions into features/step_definitions/email_steps.rb:

rails generate email_spec:steps


Now I’ll write my user story:

Scenario: Guitarist shares song with guitarist
  Given the following user exists:
    | name         | email            |
    | Eric Clapton | |
  And I sign in as ""
  And I am on the share page for "Layla"
  When I fill in "Share with" with ""
  And I press "Share Song"
  And "" opens the email
  Then he should see "Eric Clapton" in the email "From" header
  And he should see "" in the email "Reply-To" header

The “From” and “Reply-To” headers

I think the “From” and “Reply-To” headers can provide a better user experience.

I don’t set the author’s email as the “From” header because I hear it’s bad spam practice to send email on behalf of users in that way. ISPs use the From header (among other things) to determine if the originator is sending spam.

Making the feature pass

Ease my worried mind:

class Mailer < ActionMailer::Base
  def share_song(song, friend)
      from: "#{} #{}",
      subject: 'Good song'

I’ve used this format so the sender’s name shows up in the receiver’s email program:


In this case, I want Jimi to be able to reply directly to Eric, so I’ve set the “Reply-To” header to be the sender’s address. I’ve explicitly not put the sender’s name in the “Reply-To” header because that doesn’t work.

In other cases, I want the receiver to reply to the email and have that sent through the Rails app, but that’s a story for another day.


Sharpen your programming skills by completing coding exercises that are reviewed by other developers at Upcase today.