Intercepting Action Mailer Emails in Rails

Create an Interceptor Module

To intercept emails in specific environments such as development, testing, or staging, we need to write some logic to do so.

module MailInterceptor
  def self.delivering_email(message)
    # This will be the email that live emails will be sent to = ['']

    # Set the cc and bcc to be nil to avoid sending out real emails = nil
    message.bcc = nil

    # Change the from if you want a different from
    message.from = ''

    # If you want to modify the subject, do so here
    message.subject = message.subject + 'Intercepted by XXXXXXX'

Then in your initializers/action_mailer.rb, add the following snippet:


To limit per environment, use a guard clause on that same line.

Photo Credit: