在Heroku上部署Redmine時的email設定方式

最近在Heroku上架了個Redmine,用來練習怎麼使用Heroku和Redmine。

本來email設定是按照Redmine wiki上教的在config/configuration.yml中設定如下

production:
delivery_method: :smtp
 smtp_settings:
    address: "smtp.sendgrid.net"
    port: 587
    authentication: :plain
    domain: "heroku.com"
    user_name: [email protected]
    password: my_password 

但如此一來就必須把帳號密碼加入git中,不然無法發佈到Heroku上,但是大家都千交代萬交代不要把密碼commit到版本控制系統中,這不是個好辦法。

事實上,Heroku有提供設定環境變數的功能,所以把config/configuration.yml改寫如下

production:
  delivery_method: :smtp
  smtp_settings:
    address: "smtp.sendgrid.net"
    port: 587
    authentication: :plain
    domain: "heroku.com"
    user_name: <%= ENV['SENDGRID_USERNAME'] %>
    password: <%= ENV['SENDGRID_PASSWORD'] %>

不過,不過這樣設定是不會動的…真正的設定法應該是不要用yml,改把設定寫在config/environments/production.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

並且把config/configuration.yml裡面的相關設定拿掉,大概改成下面這樣

default:
  # Outgoing emails configuration (see examples above)
  email_delivery:
    delivery_method: :smtp

這樣應該就沒問題了。

Contents

comments powered by Disqus