mail_interceptor

Intercepts and forwards emails in non production environment


Keywords
gem, mail-interceptor, rails, ruby, ruby-on-rails
License
MIT
Install
gem install mail_interceptor -v 0.0.7

Documentation

Mail Interceptor

Circle CI

TOC

About

This gem intercepts and forwards email to a forwarding address in a non-production environment. It also allows to not intercept certain emails so that testing of emails is easier in development/staging environment.

Usage

# There is no need to include this gem for production or for test environment
gem 'mail_interceptor', group: [:development, :staging]
# config/initializers/mail_interceptor.rb

options = { forward_emails_to: 'intercepted_emails@domain.com',
            deliver_emails_to: ["@wheel.com"] }

unless (Rails.env.test? || Rails.env.production?)
  interceptor = MailInterceptor::Interceptor.new(options)
  ActionMailer::Base.register_interceptor(interceptor)
end

Do not use this feature in test mode so that in tests you can test against provided recipients of the email.

deliver_emails_to

Passing deliver_emails_to is optional. If no "deliver_emails_to" is passed then all emails will be intercepted and forwarded in non-production environment.

Let's say that you want to actually deliver all emails having the pattern "@BigBinary.com". Here is how it can be accomplished.

MailInterceptor::Interceptor.new({ forward_emails_to: 'intercepted_emails@domain.com',
                                   deliver_emails_to: ["@bigbinary.com"] })

If you want the emails to be delivered only if the email address is qa@bigbinary.com then that can be done too.

MailInterceptor::Interceptor.new({ forward_emails_to: 'intercepted_emails@domain.com',
                                   deliver_emails_to: ["qa@bigbinary.com"] })

Now only qa@bigbinary.com will get its emails delivered and all other emails will be intercepted and forwarded.

The regular expression is matched without case sensitive. So you can mix lowercase and uppercase and it won't matter.

forward_emails_to

This is a required field.

It can take a single email or an array of emails.

MailInterceptor::Interceptor.new({ forward_emails_to: 'intercepted_emails@bigbinary.com' })

It can also take an array of emails in which case emails are forwarded to each of those emails in the array.

MailInterceptor::Interceptor.new({ forward_emails_to: ['intercepted_emails@bigbinary.com',
                                                       'qa@bigbinary.com' })

ignore_bcc and ignore_cc

By default bcc and cc are ignored. You can pass :ignore_bcc or :ignore_cc options as false, if you don't want to ignore bcc or cc.

Custom Environment

By default all emails sent in non production environment are intercepted. However you can control this behavior by passing env as the key. It accepts any ruby objects which responds to intercept? method. If the result of that method is true then emails are intercepted otherwise emails are not intercepted.

Below is an example of how to pass a custom ruby object as value for env key.

Besides method intercept? method name is needed if you have provided subject_prefix. This name will be appended to the subject_prefix to produce something like [WHEEL STAGING] Forgot password. In this case STAGING came form name.

class MyEnv
  def name
    ENV["ENVIRONMENT_NAME"]
  end

  def intercept?
    ENV["INTERCEPT_MAIL"] == '1'
  end
end

MailInterceptor::Interceptor.new({ env: MyEnv.new,
                                   forward_emails_to: ['intercepted_emails@bigbinary.com',
                                   'qa@bigbinary.com' })

Prefixing email with subject

If you are looking for automatically prefix all delivered emails with the application name and Rails environment then we recommend using email_prefixer gem .

Brought to you by