Git Product home page Git Product logo

mail_interceptor's Introduction

MailInterceptor

Circle CI

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.

Installation

Add this line to your application's Gemfile:

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

Usage

# config/initializer/mail_interceptor.rb

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

interceptor = MailInterceptor::Interceptor.new(options)

unless (Rails.env.test? || Rails.env.production?)
  ActionMailer::Base.register_interceptor(interceptor)
end

Do not use this feature in test mode so that in your tests you can test against real 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: '[email protected]',
                                   deliver_emails_to: ["@bigbinary.com"] })

If you want the emails to be delivered only if the email address is [email protected] then that can be done too.

MailInterceptor::Interceptor.new({ forward_emails_to: '[email protected]',
                                   deliver_emails_to: ["[email protected]"] })

Now only [email protected] 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: '[email protected]' })

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: ['[email protected]',
                                                       '[email protected]' })

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: ['[email protected]',
                                   '[email protected]' })

Brought to you by

BigBinary logo

mail_interceptor's People

Contributors

neerajsingh0101 avatar bcroesch avatar billhorsman avatar vipulnsward avatar mechanicles avatar monkbroc avatar prathamesh-sonpatki avatar hlfcoding avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.