2012-06-28 8 views
6

設置我把我的ActionMailer配置在我config/environment.rb文件像這樣:的Rails的ActionMailer忽略的environment.rb

MyApp::Application.initialize! 
MyApp::Application.configure do 

    config.action_mailer.delivery_method = :smtp 
    config.action_mailer.smtp_settings = { 
     address: "smtp.elasticemail.com", 
     port: 2525, 
     domain: "myapp.com", 
     authentication: "plain", 
     user_name: "my_username", 
     password: "my_password", 
     enable_starttls_auto: true 
    } 

end 

我的理解是,這是配置將適用於所有的環境設置的正確方法。

這在開發中運行良好,但是當我部署到我的登臺服務器(使用自定義config/environments/staging.rb配置文件)時,它在嘗試傳遞郵件時遇到了「連接被拒絕」錯誤。 staging.rb根本沒有與郵件相關的設置。

所以我啓動了控制檯登臺服務器上RAILS_ENV=staging rails c,並「把Rails.application.config.action_mailer」顯示,我把environment.rb的設置確實有效,但由於某些原因的ActionMailer不使用它們。

通過實驗,我發現將配置直接複製到staging.rb可以解決問題。爲什麼是這個必要嗎?如果導軌控制檯顯示設置有效,爲什麼ActionMailer不使用它們?

挖越深,我認爲這是期望我的郵件類的DELIVERY_METHOD未設置:

MyMailer.foo(Person.find(1)).delivery_method 

=> #<Mail::SMTP:0x0000000370d4d0 @settings={:address=>"localhost", :port=>25, :domain=>"localhost.localdomain", :user_name=>nil, :password=>nil, :authentication=>nil, :enable_starttls_auto=>true, :openssl_verify_mode=>nil, :ssl=>nil, :tls=>nil}> 

回答

12

MyApp::Application.configure do 

    config.action_mailer.delivery_method = :smtp 
    config.action_mailer.smtp_settings = { 
     address: "smtp.elasticemail.com", 
     port: 2525, 
     domain: "myapp.com", 
     authentication: "plain", 
     user_name: "my_username", 
     password: "my_password", 
     enable_starttls_auto: true 
    } 

end 

MyApp::Application.initialize!

+3

之前,我真的很想看看#1 SQL記錄我的問題發佈時間和你回答(並解決)它的時間......必須<30秒。 –

+0

haha​​ha:P我想是這樣的:P –

+0

偉大的問題!很好的答案!爲我解決了一個棘手的問題。 – thisfeller