Rails ActionMailer忽略environment.rb中的设置 [英] Rails ActionMailer ignores settings in environment.rb

查看:81
本文介绍了Rails ActionMailer忽略environment.rb中的设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将ActionMailer配置放在我的 config / environment.rb 文件中,如下所示:

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.

这在开发中效果很好,但是当我部署到我的登台服务器(使用自定义的 config / environments / staging.rb 配置文件)尝试传递邮件时出现拒绝连接错误。 staging.rb 根本没有与邮件相关的设置。

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.

因此我在控制台上启动了控制台。带有 RAILS_ENV = staging rails c 的临时服务器,并且 puts Rails.application.config.action_mailer显示我在 environment.rb <中设置的设置/ code>确实有效,但是由于某些原因,ActionMailer未使用它们。

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.

通过实验,我发现将配置直接复制到 staging.rb 可以解决问题。为什么这是必要的?如果rails控制台显示设置有效,那么为什么ActionMailer不使用它们?

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?

更深入地研究,我发现我的邮件程序类的delivery_method没有按预期设置:

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


推荐答案

放置

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之前!

这篇关于Rails ActionMailer忽略environment.rb中的设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆