Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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).

like image 733
Newy Avatar asked Jun 16 '10 22:06

Newy


3 Answers

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.

like image 91
Alex Korban Avatar answered Oct 21 '22 21:10

Alex Korban


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

like image 42
IanBussieres Avatar answered Oct 21 '22 23:10

IanBussieres


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 <[email protected]>']
          # remove bcc, cc, etc.
        end
      end
      ActionMailer::Base.register_interceptor(TestEmailsInterceptor)
    end
    
like image 43
Cyril Duchon-Doris Avatar answered Oct 21 '22 22:10

Cyril Duchon-Doris