ActionMailer设置在开发环境和生产环境中不正确的差异

9

错误

我已经在开发环境中完美地设置了我的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>'

问题到目前为止

我可以提供的最有价值的信息可能是:在生产环境中,消息对象上的实际 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}>

这似乎只是默认设置,如ActionMailer::DeliveryMethods的第22行所定义的,而不是来自我的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

config/environments/production.rb:

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

请注意,我还尝试在development.rb中注释掉两行额外的代码(并尝试将它们添加到production.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
2个回答

7

我找到了答案,但这似乎与我以为的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)

我仍然不确定前者在开发环境中可行,但在生产环境中并不如此,但后者现在在我的生产环境中可以使用,所以我现在使用的是后者。


2
感谢您发布解决方案。我也遇到了这个确切的问题! - lambinator
没问题,很高兴我能为你节省一些时间! 严肃地说,我花了整整一天的时间才找到它。 :/ - Matt Huggins

2
你的`environment.rb`文件设置是否像下面这样:
Myapp::Application.initialize!
Myapp::Application.configure do { }

如果是这样,请交换这两行。我曾经遇到过类似的问题(邮件配置在开发环境中可以工作,但在其他环境中无法工作),那就是问题所在。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接