gpt4 book ai didi

configuration - ActionMailer 设置在开发和生产之间存在错误差异

转载 作者:行者123 更新时间:2023-12-01 17:16:03 26 4
gpt4 key购买 nike

错误

我已经完成了 ActionMailer 设置并在开发中完美运行。我可以调用 UserMailer.welcome(user).deliver,然后电子邮件就会到达目的地。但是,当我将代码投入生产并调用相同的 deliver 方法时,突然出现错误:

Errno::ECONNREFUSED: Connection refused - connect(2)
from /usr/local/rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/smtp.rb:546:in `initialize'
from /usr/local/rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/smtp.rb:546:in `open'
from /usr/local/rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/smtp.rb:546:in `tcp_socket'
from /usr/local/rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/smtp.rb:555:in `block in do_start'
from /usr/local/rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/timeout.rb:58:in `timeout'
from /usr/local/rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/timeout.rb:89:in `timeout'
from /usr/local/rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/smtp.rb:555:in `do_start'
from /usr/local/rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/smtp.rb:525:in `start'
from /webapps/myapp/production/shared/bundle/ruby/1.9.1/gems/mail-2.3.0/lib/mail/network/delivery_methods/smtp.rb:128:in `deliver!'
from /webapps/myapp/production/shared/bundle/ruby/1.9.1/gems/mail-2.3.0/lib/mail/message.rb:1989:in `do_delivery'
from /webapps/myapp/production/shared/bundle/ruby/1.9.1/gems/mail-2.3.0/lib/mail/message.rb:230:in `block in deliver'
from /webapps/myapp/production/shared/bundle/ruby/1.9.1/bundler/gems/rails-5680a51dcbaf/actionmailer/lib/action_mailer/base.rb:414:in `block in deliver_mail'
from /webapps/myapp/production/shared/bundle/ruby/1.9.1/bundler/gems/rails-5680a51dcbaf/activesupport/lib/active_support/notifications.rb:55:in `block in instrument'
from /webapps/myapp/production/shared/bundle/ruby/1.9.1/bundler/gems/rails-5680a51dcbaf/activesupport/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /webapps/myapp/production/shared/bundle/ruby/1.9.1/bundler/gems/rails-5680a51dcbaf/activesupport/lib/active_support/notifications.rb:55:in `instrument'
from /webapps/myapp/production/shared/bundle/ruby/1.9.1/bundler/gems/rails-5680a51dcbaf/actionmailer/lib/action_mailer/base.rb:412:in `deliver_mail'
from /webapps/myapp/production/shared/bundle/ruby/1.9.1/gems/mail-2.3.0/lib/mail/message.rb:230:in `deliver'
from (irb):10
from /webapps/myapp/production/shared/bundle/ruby/1.9.1/bundler/gems/rails-5680a51dcbaf/railties/lib/rails/commands/console.rb:45:in `start'
from /webapps/myapp/production/shared/bundle/ruby/1.9.1/bundler/gems/rails-5680a51dcbaf/railties/lib/rails/commands/console.rb:8:in `start'
from /webapps/myapp/production/shared/bundle/ruby/1.9.1/bundler/gems/rails-5680a51dcbaf/railties/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

到目前为止的问题

我能提供的最有值(value)的信息可能是 Message 对象上的实际 delivery_method 数据在生产中不正确。在开发中,当我调用 UserMailer.welcome(user).delivery_method 时,(格式化的)输出为:

#<Mail::SMTP:0x000001042c4a20 @settings={
:address=>"smtp.gmail.com",
:port=>587,
:domain=>"foobar.com",
:user_name=>"example@foobar.com",
:password=>"MY_PASSWORD",
:authentication=>"plain",
:enable_starttls_auto=>true,
:openssl_verify_mode=>nil}>

这显然与我在 mailers.yml 中定义的设置匹配。在生产中,当我进行相同的调用时,输出是:

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

这似乎只是 line 22 of ActionMailer::DeliveryMethods 上定义的默认值而不是我自己的 mailers.yml 设置。

相关代码

据我所知,我的环境应该有相同的 ActionMailer 设置。

config/environment.rb:

Myapp::Application.config.action_mailer.delivery_method = :smtp
Myapp::Application.config.action_mailer.smtp_settings = YAML.load_file(
Rails.root.join('config', 'mailers.yml'))[Rails.env].to_options

config/mailers.yml:

default: &default
address: smtp.gmail.com
port: 587
domain: foobar.com
user_name: example@foobar.com
password: MY_PASSWORD
authentication: plain
enable_starttls_auto: true

development:
<<: *default

production:
<<: *default

config/environments/development.rb:

Myapp::Application.configure do
# ...
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.interceptors = ['MailInterceptor']
# ...
end

配置/环境/生产.rb:

Myapp::Application.configure do
# ...
config.action_mailer.default_url_options = { :host => 'foobar.com' }
# ...
end

请注意,我还尝试注释掉development.rb中的两行额外内容(并尝试将它们添加到生产.rb中)而不进行任何更改 - 我在生产中仍然遇到相同的错误,但在开发中却没有。

此外,虽然我认为它不相关,但我将包含我在development.rb中引用的MailIntercepter(它只是将所有邮件重定向到我的电子邮件地址,而不是测试用户的电子邮件地址):

class MailInterceptor
def self.delivering_email(message)
message.subject = "[#{message.to}] #{message.subject}"
message.to = "example+catcher@foobar.com"
end
end

最佳答案

弄清楚了,尽管它似乎违背了我认为的 Rails 3.1 配置标准方法。

更改了这一行:

Myapp::Application.config.action_mailer.smtp_settings = YAML.load_file(
Rails.root.join('config', 'mailers.yml'))[Rails.env].try(:to_options)

对此:

ActionMailer::Base.smtp_settings = YAML.load_file(
Rails.root.join('config', 'mailers.yml'))[Rails.env].try(:to_options)

仍然不确定为什么前者适用于开发而不适用于生产,但后者现在适用于我的生产,所以这就是我现在要使用的。

关于configuration - ActionMailer 设置在开发和生产之间存在错误差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6991030/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com