Last Updated: February 25, 2016
·
697
· ttoni

Mailing in staging environment

When in staging environment, one does not flood the user's inbox with pointless staging emails. A good option is to redirect all emails sent while in staging to a specific email address.

Based on these two approaches: DHH and thoughtbot. And using the gem recipient_interceptor.

First add recipient_interceptor to Gemfile.

Example of a production config file:

Appname::Application.configure do

  config.action_mailer.default_url_options={host:'www.mypharmacash.com'}
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.smtp_settings = {
    :address   => "smtp.mandrillapp.com",
    :port      => 25, # ports 587 and 2525 are also supported with STARTTLS
    :enable_starttls_auto => true, # detects and uses STARTTLS
    :user_name => ENV['MANDRILL_USERNAME'],
    :password  => ENV['MANDRILL_PASSWORD'], # SMTP password is any valid API key
    :authentication => 'login', # Mandrill supports 'plain' or 'login'
    :domain => 'www.domainname.com', # your domain to identify your server when connecting
  }

end

And this would be the staging config file:

# Based on production defaults
require Rails.root.join("config/environments/production")

Mail.register_interceptor RecipientInterceptor.new(
  ENV['STAGING_EMAIL_RECIPIENTS'],
  subject_prefix: '[STAGING]'
)