2013-03-06 121 views
0

我的ActionMailer在生產和開發中都能正常工作。我針對每個環境使用不同的smtp設置,針對開發的gmail和通過Heroku進行生產的SendGrid帳戶。我手動切換setup_mail.rb文件中的設置以在開發中工作,然後在投入生產之前切換它們。這可以防止我的Gmail密碼成爲公共GitHub上的SendGrid/Heroku的設置不要求我在文件中的密碼:如何爲開發和生產設置不同的ActionMailer基本smtp設置?

發展爲setup_mail.rb

ActionMailer::Base.smtp_settings = { 
     :address    => "smtp.gmail.com", 
     :port     => 587, 
     :domain    => "mysite.com", 
     :user_name   => "[email protected]", 
     :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基本smtp設置?謝謝

回答

0

在production.rb和development.rb中有這個設置,而不是讓你的密碼被硬編碼,你也可以在本地使用環境變量,在你的項目中創建一個.env文件,當你在cd

[email protected] 
EMAIL_PASSWORD= mypassword 

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

+0

感謝,環境變量是要走的路。在開發和生產過程中,我放棄了使用SendGrid的gmail設置,並且我的設置是安全的。我發現這個[RailsApps Project Rails Environment Variables](http://railsapps.github.com/rails-environment-variables.html)和RailsCast#85 YAML配置(修訂版)非常有幫助。還有我沒有使用過的Figaro Gem,但是懷疑在這裏也很有用。 – 2013-03-07 20:39:13

相關問題