Safe emailing for Rails
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

Code Climate Build Status


Safe emailing for Rails. Prevents you from sending emails to real customers from non-production environments. You can choose to either re-route all emails to a single developer account or allow only recipients of your own domain.


Add this line to your application's Gemfile:

gem 'mailsafe'

And then execute:

$ bundle


You can re-route all emails to your developer email account by adding this your development.rb file

Mailsafe.setup do |config|
  config.override_receiver = ""

The email subjects will be delivered to your account and prefixed with the original receiver in square brackets.

You can send emails to the intended recipients but only to a whitelist of receipient domains (e.g. helpful for a staging environment where you want to send emails to your company's and your client's domain)

Mailsafe.setup do |config|
  config.allowed_domain = ","

If you are running multiple environments with mailsafe it can help to know which environment an email came from. You can have mailsafe prefix the subject line with [environment name]

config.prefix_email_subject_with_rails_env = true


In order to filter (as in not send) emails I had to monkey patch the Mail#deliver method. If this makes you feel uneasy, better try out a different solution.


  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request