Rails Mailer"Net :: OpenTimeout:execution expired"仅限生产服务器上的异常

mav*_*tic 35 smtp ruby-on-rails actionmailer exception-notification

我在Ubuntu 12.04 TLS VPS上使用Ruby MRI 2.0.0和Rails 3.2.12,并尝试在我的应用程序中设置电子邮件通知.几天前工作正常,但现在不行了.我的网站主机是OVH.

我的SMTP设置:

config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true

ActionMailer::Base.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => 587,
  :user_name            => 'sender@gmail.com',
  :password             => 'secret',
  :authentication       => 'plain',
  :enable_starttls_auto => true
}
Run Code Online (Sandbox Code Playgroud)

使用RAILS_ENV=production rails console:

class MyMailer < ActionMailer::Base
  def test_email
    sender     = "sender@gmail.com"
    receiver   = "receiver@example.com"
    mail from: sender, to: receiver, subject: "Hello!", body: "World!!"
  end
end
 => nil

MyMailer.test_email.deliver
Run Code Online (Sandbox Code Playgroud)

输出:

Net::OpenTimeout: execution expired
    from ~/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:540:in `initialize'
    from ~/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:540:in `open'
    from ~/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:540:in `tcp_socket'
    from ~/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:550:in `block in do_start'
    from ~/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:549:in `do_start'
    from ~/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:519:in `start'
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/mail-2.4.4/lib/mail/network/delivery_methods/smtp.rb:144:in `deliver!'
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/mail-2.4.4/lib/mail/message.rb:2034:in `do_delivery'
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/mail-2.4.4/lib/mail/message.rb:229:in `block in deliver'
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/actionmailer-3.2.12/lib/action_mailer/base.rb:415:in `block in deliver_mail'
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/activesupport-3.2.12/lib/active_support/notifications.rb:123:in `block in instrument'
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/activesupport-3.2.12/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/activesupport-3.2.12/lib/active_support/notifications.rb:123:in `instrument'
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/actionmailer-3.2.12/lib/action_mailer/base.rb:413:in `deliver_mail'
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/mail-2.4.4/lib/mail/message.rb:229:in `deliver'
    from (irb):28
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/railties-3.2.12/lib/rails/commands/console.rb:47:in `start'
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/railties-3.2.12/lib/rails/commands/console.rb:8:in `start'
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/railties-3.2.12/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'2.0.0p0 :029 >
Run Code Online (Sandbox Code Playgroud)

我尝试了以下方法:

  • exception_notification几天前,宝石被添加到设置中.我试着评论它的线Gemfile以及它的匹配配置,然后运行bundle install.重新启动服务器后,即使我删除并重新创建gemset,问题仍然存在.
  • 在虚拟机上测试它(与VPS完全相同的设置,包括iptables规则):有效
  • 禁用iptables规则:不起作用
  • 使用openssl手动从VPS连接到Gmail:工作(因此这不是防火墙问题 - 请参阅此处:通过命令行连接到smtp.gmail.com);
  • 在Gmail帐户选项中启用IMAP(已禁用):不起作用
  • 使用其他Gmail帐户:不起作用
  • 用Ruby 1.9.3替换Ruby 2.0.0
  • 升级到Rails 3.2.13

有人可能有关于如何解决此问题的线索吗?

谢谢!

Zip*_*pie 17

我可能有同样的问题,我的生产应用程序没有发送邮件,虽然开发中的一切都运行良好.我也得到了"Net :: OpenTimeout"错误.

我的问题是我在生产中使用了Google服务器,它在出站连接上阻止了端口25,465和587.

由于我使用Mandrill发送邮件,我能够将连接端口从587切换到2525,现在一切正常.

  • 我也使用谷歌云,并将我的smtp端口从587改为2525,工作正常!谢谢! (2认同)

Dan*_*iro 15

首先,与Telnet直接连接:

telnet smtp-relay.sendinblue.com 587
Trying 94.143.17.4...
Run Code Online (Sandbox Code Playgroud)

这是基本连接故障排除,适用于任何提供商或端口.用您的实际主机名/端口替换SendBlue和587端口.

如果您收到此错误:

telnet: Unable to connect to remote host: Connection timed out
Run Code Online (Sandbox Code Playgroud)

那么,问题不在于Rails.

在上面的示例中,问题出在端口号中.sendinblue或mandrill等服务(我相信gmail也不再支持587端口)."2525"是新的"587".


如果你在telnet上超时,请检查:

  1. 主机名:通常人们使用"smtp.sendinblue.com"而不是"stmp-relay.sendinblue.com","smtp.mandrill.com"而不是"smtp.mandrillapp.com",等等.
  2. 端口:587已过时.主要提供商现在使用2525而不是.像DigitalOcean这样的主要云服务也阻止了对587的传出连接.这就是为什么它可以在您的电脑上运行,但不在您的服务器上.我甚至不会提到"25"端口,它比587更加过时.此外,一些提供商使用特定的非默认端口或者imap.
  3. ipv6 vs ipv4:检查主机名是否被转换为IPv4.如果没有,请尝试禁用IPv6(请参阅其他答案).
  4. hostname resolution:在您知道电子邮件发送正在运行的计算机上运行相同的telnet命令.检查翻译的ip("试图xxx ..."的xxx部分)是否相同.如果没有,请返回到您的服务器并用此ip替换主机名.如果有效,请更改/ etc/hosts并强制主机名使用此ip.


Dar*_*rme 13

这里还有一个临时修复,可能会在等待您的托管服务提供商解决问题时派上用场:

将以下行添加到/etc/sysctl.conf:

#disable ipv6
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
Run Code Online (Sandbox Code Playgroud)

现在,应用程序可以再次发送电子邮件.

您始终可以知道是否已启用IPv6呼叫

cat /proc/sys/net/ipv6/conf/all/disable_ipv6
Run Code Online (Sandbox Code Playgroud)

从终端.两个可能的答案:0 =>启用IPv6; 1 => IPv6已禁用.

来自:https://serverfault.com/questions/512744/timeout-error-in-all-my-apps-for-every-call-to-smtp-servers


mav*_*tic 5

问题是由于生产服务器上的IPv6配置错误,现在已得到修复.


Sag*_*ani 5

您可以将Ubuntu配置为首选IPv4 over IPv6.这样,您就可以发送电子邮件并访问仅限IPv6的站点.编辑/etc/gai.conf并取消注释以下行:

precedence ::ffff:0:0/96 100
Run Code Online (Sandbox Code Playgroud)