Brock90
Brock90

Reputation: 814

Sending email only in prodcution but not in development with Rails

I have an app that sends email and I configured mandrill in my application.rb file.

config.action_mailer.smtp_settings = {
  address:              'smtp.mandrillapp.com',
  port:                 587,
  domain:               'translatr.herokuapp.com',
  user_name:            ENV["MANDRILL_USERNAME"], 
  password:             ENV["MANDRILL_PASSWORD"], 
  authentication:       'plain',
  enable_starttls_auto: true  }

However I don't want to send out mail during development and when testing but I do in production.Do I do this by moving this setting to production.rb?or is there another way to do it?

Upvotes: 0

Views: 749

Answers (3)

spickermann
spickermann

Reputation: 107117

You want to have this setting in production.rb only. Because there are other useful settings for the other environments:

In test.rb you will find something like:

# Tell Action Mailer not to deliver emails to the real world.
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test

This allows you to write specs that check if a mail would have been send on production by checking if ActionMailer::Base.deliveries.size increased.

And the development environment usually should not sent an email. It just logs into development.rb what email would be send on production. Also very useful for debugging.

Upvotes: 1

user2968358
user2968358

Reputation: 1

Instead of changing the setting in application.rb you can check for current rails environment. If env is production call the mail method.

      send_mail if Rails.env.production?

How to tell if rails is in production?

Upvotes: 0

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

Move your settings to production.rb, then email will be sent in production only

Upvotes: 1

Related Questions