Rails ActionMailer 忽略environment.rb 中的设置

2024-04-01

我把我的 ActionMailer 配置放在我的config/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,和“puts Rails.application.config.action_mailer”显示我放入的设置environment.rb确实有效,但由于某种原因 ActionMailer 没有使用它们。

通过实验我发现直接将配置复制到staging.rb解决问题。为什么这是必要的?如果 Rails 控制台显示设置已生效,为什么 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}> 

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!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Rails ActionMailer 忽略environment.rb 中的设置 的相关文章

随机推荐