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.

We’ll use 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