仅在生产中发送电子邮件,但不使用Rails在开发中发送

时间:2013-11-08 09:24:15

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2

我有一个发送电子邮件的应用,我在application.rb文件中配置了mandrill。

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  }

但是我不希望在开发期间和测试时发送邮件,但是我在生产中。我是通过将此设置移动到production.rb来实现的吗?还是有其他方法可以做到这一点?

3 个答案:

答案 0 :(得分:1)

将您的设置移至production.rb,然后电子邮件将仅在生产中发送

答案 1 :(得分:1)

您希望仅在production.rb中设置此设置。因为其他环境还有其他有用的设置:

test.rb中,你会发现类似的内容:

# 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

这允许您通过检查ActionMailer::Base.deliveries.size是否增加来编写检查邮件是否已在生产中发送的规范。

开发环境通常不应该发送电子邮件。它只会登录development.rb生产时发送的电子邮件。对调试也非常有用。

答案 2 :(得分:0)

您可以检查当前的rails环境,而不是更改application.rb中的设置。如果env是生产调用mail方法。

      send_mail if Rails.env.production?

How to tell if rails is in production?