---
title: Delivering All Email From Staging To A Group Email Address
teaser:
tags: web,ruby,rails
author: Dan Croak
published_on: 2013-01-18
---

All email from the staging environment of a Rails app can be intercepted and
delivered to a group email address. This avoids accidentally delivering staging
email to production customers and lets the product team see all the emails that
are being sent to customers.

## Configure

We'll use
[`recipient_interceptor`](https://github.com/croaky/recipient_interceptor).

`Gemfile`:

    gem 'recipient_interceptor'

`config/initializers/mail.rb`:

    if Rails.env.staging? || Rails.env.production?
      MAIL_SETTINGS = {
        address: 'smtp.sendgrid.net',
        authentication: :plain,
        domain: 'heroku.com',
        password: ENV['SENDGRID_PASSWORD'],
        port: '587',
        user_name: ENV['SENDGRID_USERNAME']
      }
    end

`config/environments/{staging,production}.rb`:

    require Rails.root.join('config/initializers/mail')

    My::Application.configure do
      config.action_mailer.delivery_method = :smtp
      config.action_mailer.smtp_settings = MAIL_SETTINGS
    end

Also in `config/environments/staging.rb`:

    Mail.register_interceptor RecipientInterceptor.new(ENV['EMAIL_RECIPIENTS'])

Use the `ENV['EMAIL_RECIPIENTS']` environment variable to update the list of
email addresses that should receive staging emails. For example:

    heroku config:add EMAIL_RECIPIENTS="staging@example.com" --remote staging

## Gmail filter

Depending on the app, this may generate thousands of emails a day. Avoid
spamming yourself by setting up a Gmail filter for emails sent to
`staging@example.com`.
