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