Rails ActionMailer ignores settings in environment.rb

不打扰是莪最后的温柔 提交于 2019-12-07 02:28:52

问题


I put my ActionMailer config in my config/environment.rb file like so:

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

My understanding is that this is the correct way to configure settings that will apply to all your environments.

This worked fine in development, but when I deployed to my staging server (which uses a custom config/environments/staging.rb config file) I got a "connection refused" error when it tried to deliver mail. staging.rb has no mailer-related settings in it at all.

So I fired up the console on the staging server with RAILS_ENV=staging rails c, and "puts Rails.application.config.action_mailer" shows that the settings I put in environment.rb are indeed in effect, but for some reason ActionMailer is not using them.

Through experimentation I found that copying the config directly into staging.rb solves the problem. Why is this necessary? If the rails console shows the settings are in effect, why isn't ActionMailer using them?

Digging deeper, I see that my mailer class's delivery_method is not set as expected:

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}> 

回答1:


put

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

before MyApp::Application.initialize!



来源:https://stackoverflow.com/questions/11252087/rails-actionmailer-ignores-settings-in-environment-rb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!