How do I configure Rails to disable sending real emails out while in staging?

0 votes
asked Jun 16, 2010 by newy

I'm on Heroku, and emails don't get sent out in development, but are properly being sent in production. I'd like to run a seperate staging instance on Heroku, but don't want emails being sent out (just to a log).

7 Answers

0 votes
answered Jan 16, 2010 by chiwai-chan

put this in your environment.rb file

config.action_mailer.delivery_method = :test

It should stop sending mail to the mail server, I think there is a :log option, but I have not tried it out.

0 votes
answered Jun 16, 2010 by alex-korban

This line in test.rb tells ActionMailer not to deliver emails:

config.action_mailer.delivery_method = :test

Instead, they are accumulated in the ActionMailer::Base.deliveries array.

You'll need to set up a staging environment for your application and configure Heroku to use that environment on your staging instance.

0 votes
answered Jan 8, 2012 by cluesque

Applications that use the Mail gem (including rails >= 3.0 projects) can use the safety_mailer gem. Specify a domain (or set of domains, or magic word in email address) email is allowed to go to, and email to all other domains is silently dropped.

https://github.com/cluesque/safety_mailer

Add the gem to your Gemfile, specifying groups (probably not production) to include it in.

gem "safety_mailer", :group => :development

Don't forget to bundle install to install

In your environment file config/environments/development.rb configure it, and some regular expressions.

config.action_mailer.delivery_method = :safety_mailer
config.action_mailer.safety_mailer_settings = {
  allowed_matchers: [ /mydomain.com/, /mytestacct@gmail.com/, /super_secret_test/ ],
  delivery_method: :smtp,
  delivery_method_settings: {
    :address => "smtp.mydomain.com",
    :port => 25,
    :domain => "mydomain.com",
    :authentication => :plain,
    :user_name => "mydomain_mailer@mydomain.com",
    :password => "password"
  }
}

... and now, email to anyone@mydomain.com, mytestacct@gmail.com, bob+super_secret_test@yahoo.com all get sent and email to other recipients (like the real users in the production database you copied to a test server) is suppressed.

0 votes
answered Jan 23, 2012 by gregolsen

You might be interested in mailtrap.io (disclaimer: I am affiliated with this product). It is a perfect tool to test email deliveries in development and production. All you have to do is set mailtrap.io as an smtp server in your staging environment config:

  config.action_mailer.smtp_settings = {
   :address => "mailtrap.io",
   :port => 2525,
   :authentication => :plain,
   :user_name => "LOGIN",
   :password => "PASSWORD"
  }

Having this all your test emails sent in staging env will be stored in mailtrap for view and sharing. But non of them will be sent to the real addresses. You can use it in development as well. And by way - it's totally free!

0 votes
answered Jan 20, 2014 by user3554642

I see people suggest using Mailtrap.io. Good alternative is Debug Mail. Using is quite simple.

0 votes
answered Sep 15, 2017 by cyril-duchon-doris

Depending on your choices

  • If you want a convenient way of receiving emails for debugging, etc. I recommend https://github.com/fgrehm/letter_opener_web, which will save emails locally, and provide an URL to browse emails that were sent. No email is sent outside, and you can very conveniently see the output in your browser

  • If you want to be able to open email files with your email clients, you should choose a :file adapter for ActionMailer (configure in config/environments/your_env.rb)

  • If you want a real production-like environment, I'd suggest to configure an email interceptor that would rewrite the TO/CC/BCC to a real mailbox of your choice, this way you can keep and test your original ActionMailer adapter

    if Rails.env.staging?
      class TestEmailsInterceptor
        def self.delivering_email(mail)
          mail.to = ['My Test Box <test-box@example.com>']
          # remove bcc, cc, etc.
        end
      end
      ActionMailer::Base.register_interceptor(TestEmailsInterceptor)
    end
    
0 votes
answered Sep 15, 2017 by ianbussieres

We use maildev, which you can install locally. Great for development and staging environments, easy to install in a variety of tech stacks.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...