Last Updated: October 26, 2022
·
1.457K
· nfedyashev

Set staging environment email to go to a single email address

Set staging environment email to go to a single email address you control in order to not accidentally send production email addresses staging data.

module SingleRecipientSmtp
  def self.included(klass)
    klass.class_eval do
      cattr_accessor :single_recipient_smtp_settings
    end
  end

  def perform_delivery_single_recipient_smtp(mail)
    mail.to = single_recipient_smtp_settings[:to]
    mail.cc = nil
    mail.bcc = nil
    perform_delivery_smtp mail
  end
end

Update your config/environments/staging.rb:

config.action_mailer.delivery_method = :single_recipient_smtp
ActionMailer::Base.send :include, SingleRecipientSmtp
ActionMailer::Base.single_recipient_smtp_settings = {
  :to => 'staging@example.com'
}

1 Response
Add your response

An even better way (in my opinion) is to use an interceptor. Check out the "Interceptors" section of the ASCIIcast here: http://railscasts.com/episodes/206-action-mailer-in-rails-3?view=asciicast

over 1 year ago ·