【问题标题】:How do I setup different ActionMailer Base smtp settings for development and production?如何为开发和生产设置不同的 ActionMailer Base smtp 设置?
【发布时间】:2013-02-21 15:37:32
【问题描述】:

我的 ActionMailer 在生产和开发中都能正常工作。我为每个环境使用不同的 smtp 设置,用于开发的 gmail 和通过 Heroku 用于生产的 SendGrid 帐户。我手动切换 setup_mail.rb 文件中的设置以在开发中工作,然后在投入生产之前将它们切换回来。这可以防止我的 gmail 密码在 github 上公开,因为 SendGrid/Heroku 设置在文件中不需要我的密码:

开发设置_mail.rb

    ActionMailer::Base.smtp_settings = {
      :address              => "smtp.gmail.com",
      :port                 => 587,
      :domain               => "mysite.com",
      :user_name            => "me@mysite.com",
      :password             => 'mypassword',
      :authentication       => "plain",
      :enable_starttls_auto => true
}

生产 setup_mail.rb

ActionMailer::Base.smtp_settings = {
  :address        => 'smtp.sendgrid.net',
  :port           => '587',
  :authentication => :plain,
  :user_name      => ENV['SENDGRID_USERNAME'],
  :password       => ENV['SENDGRID_PASSWORD'],
  :domain         => 'heroku.com'
}
ActionMailer::Base.delivery_method = :smtp

我担心我会不小心将带有密码的开发设置推送到 github。我想停止手动切换设置以防止这种情况发生。如何为开发和生产设置不同的 ActionMailer Base smtp 设置?谢谢

【问题讨论】:

    标签: ruby-on-rails email smtp sendmail actionmailer


    【解决方案1】:

    在 production.rb 和 development.rb 中有这个设置,而不是硬编码你的密码,你也可以在本地使用环境变量,在你的项目中创建一个 .env 文件,当你 cd 时加载它:

    EMAIL=me@mysite.com
    EMAIL_PASSWORD= mypassword
    

    在 development.rb 中使用 ENV['EMAIL'] AND ENV['EMAIL_PASSWORD']

    【讨论】:

    • 谢谢,环境变量是要走的路。我在开发和生产中都放弃了 gmail 设置,转而使用 SendGrid,我的设置是安全的。我发现这个 RailsApps Project Rails Environment Variables 以及 RailsCast #85 YAML 配置(修订版)非常有用。还有一个 Figaro Gem,我没用过,但怀疑在这里也有用。
    猜你喜欢
    • 2011-10-22
    • 2012-10-08
    • 2014-01-06
    • 2017-12-20
    • 2017-08-18
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 2016-04-07
    相关资源
    最近更新 更多