Ruby-on-rails Rails Mailer "Net::OpenTimeout: execution expired" 仅生产服务器上的异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16040158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails Mailer "Net::OpenTimeout: execution expired" Exception on production server only
提问by mavenastic
I am using Ruby MRI 2.0.0 and Rails 3.2.12 on a Ubuntu 12.04 TLS VPS and attempting to setup email notifications in my app. It was working fine a few days ago, but not anymore.My web host is OVH.
我在 Ubuntu 12.04 TLS VPS 上使用 Ruby MRI 2.0.0 和 Rails 3.2.12,并尝试在我的应用程序中设置电子邮件通知。几天前它工作正常,但现在不行了。我的虚拟主机是 OVH。
My SMTP settings:
我的 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 => '[email protected]',
:password => 'secret',
:authentication => 'plain',
:enable_starttls_auto => true
}
Using RAILS_ENV=production rails console:
使用RAILS_ENV=production rails console:
class MyMailer < ActionMailer::Base
def test_email
sender = "[email protected]"
receiver = "[email protected]"
mail from: sender, to: receiver, subject: "Hello!", body: "World!!"
end
end
=> nil
MyMailer.test_email.deliver
The output:
输出:
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 >
I tried the following:
我尝试了以下方法:
- The
exception_notificationgem was added to the setup a few days ago. I tried to comment its line inGemfileas well as its matching configuration, and runbundle install. After restarting the server, the issue is still present, even if I delete and recreate the gemset. - Test it out on a virtual machine (exact same setup as the VPS including iptables rules): works
- Disable iptables rules: does not work
- Manually connect to Gmail from the VPS using openssl: works(so this is not a firewall issue - see here: Connecting to smtp.gmail.com via command line);
- Enable IMAP in Gmail account options (it was disabled): does not work
- Use a different Gmail account: does not works
- Replace Ruby 2.0.0 by Ruby 1.9.3
- Upgrade to Rails 3.2.13
exception_notification几天前,gem 已添加到设置中。我试图注释它的行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
Does someone have a possible clue as to how to resolve this issue?
有人对如何解决这个问题有可能的线索吗?
Thanks!
谢谢!
采纳答案by mavenastic
The issue was due to an IPv6 misconfiguration on the production server and has now been fixed.
该问题是由于生产服务器上的 IPv6 配置错误造成的,现已修复。
回答by Zippie
I probably had the same issue, my production application didn't send mails although everything in development was working fine. I also got the "Net::OpenTimeout" error.
我可能遇到了同样的问题,尽管开发中的一切正常,但我的生产应用程序没有发送邮件。我也收到了“Net::OpenTimeout”错误。
My problem was that i was using a Google server in production, and it blocks ports 25, 465 and 587 on outbound connections.
我的问题是我在生产中使用了 Google 服务器,它阻止了出站连接上的端口 25、465 和 587。
Since I was using Mandrill for sending mails, I was able to switch the connecting port from 587 to 2525 and everything is okay now.
由于我使用 Mandrill 发送邮件,我能够将连接端口从 587 切换到 2525,现在一切正常。
回答by Daniel Loureiro
First, do a direct connection with Telnet:
首先用Telnet做直连:
telnet smtp-relay.sendinblue.com 587
Trying 94.143.17.4...
This is the basic connection troubleshooting, and works with any provider or port. Replace SendBlue and the 587 port with your actual hostname/port.
这是基本的连接故障排除,适用于任何提供商或端口。用您的实际主机名/端口替换 SendBlue 和 587 端口。
If you get this error:
如果您收到此错误:
telnet: Unable to connect to remote host: Connection timed out
then, the problem isn't in Rails.
那么,问题不在于 Rails。
In the above example, the problem is in the port number. Services like sendinblue or mandrill (I believe gmail too) don't support the 587 port anymore. "2525" is the new "587".
在上面的例子中,问题出在端口号上。sendinblue 或 mandrill(我也相信 gmail)之类的服务不再支持 587 端口。“2525”是新的“587”。
If you get a timeout on telnet, check this:
如果您在 telnet 上超时,请检查以下内容:
- hostname: it's usual to people use "smtp.sendinblue.com" instead of "stmp-relay.sendinblue.com", "smtp.mandrill.com" instead of "smtp.mandrillapp.com", and so on.
- port: 587 is obsolete. Major providers are now using 2525 instead. Major cloud services like DigitalOcean, block outgoing connections to 587as well. That's why it will work on your pc, but not on your server. I won't even mention the "25" port, which is even more obsolete than 587. Also, some providers use specific non-default ones instead or imap.
- ipv6 vs ipv4: check if the hostname is being translated as a IPv4. If not, try to disable IPv6 (see others answers).
- hostname resolution: run the same telnet command on a machine that you know the email sending is working. Check if the translated ip (the xxx part of "Trying xxx...") is the same. If not, go back to your server and replace the hostname with this ip. If works, change your /etc/hosts and force the hostname to use this ip.
- 主机名:人们通常使用“smtp.sendinblue.com”而不是“stmp-relay.sendinblue.com”、“smtp.mandrill.com”而不是“smtp.mandrillapp.com”等等。
- 端口:587 已过时。主要供应商现在使用 2525 代替。DigitalOcean 等主要云服务也会阻止与 587 的传出连接。这就是为什么它可以在您的 PC 上运行,但不能在您的服务器上运行。我什至不会提到“25”端口,它比 587 更陈旧。此外,一些提供商使用特定的非默认端口或 imap。
- ipv6 vs ipv4:检查主机名是否被转换为 IPv4。如果没有,请尝试禁用 IPv6(请参阅其他答案)。
- 主机名解析:在您知道正在发送电子邮件的机器上运行相同的 telnet 命令。检查翻译后的ip(“尝试xxx...”的xxx部分)是否相同。如果没有,请返回您的服务器并用此 ip 替换主机名。如果有效,请更改您的 /etc/hosts 并强制主机名使用此 IP。
回答by Darme
Here is also a temporary fix that may come in handy while waiting for your hosting provider to fix the issue:
这里还有一个临时修复程序,在等待您的托管服务提供商解决问题时可能会派上用场:
Add the following lines to /etc/sysctl.conf:
将以下行添加到/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
Now the apps are able to send emails again.
现在这些应用程序可以再次发送电子邮件了。
You can always know if IPv6 is enabled calling
您始终可以知道是否启用了 IPv6 呼叫
cat /proc/sys/net/ipv6/conf/all/disable_ipv6
from terminal. Two possible answers: 0 => IPv6 is enabled; 1 => IPv6 disabled.
从终端。两个可能的答案:0 => IPv6 已启用;1 => IPv6 已禁用。
From: https://serverfault.com/questions/512744/timeout-error-in-all-my-apps-for-every-call-to-smtp-servers
来自:https: //serverfault.com/questions/512744/timeout-error-in-all-my-apps-for-every-call-to-smtp-servers
回答by Sagar Ranglani
You can configure Ubuntu to prefer IPv4 over IPv6. This way you will be able to send emails and access IPv6-only sites. Edit /etc/gai.confand uncomment the following line:
您可以将 Ubuntu 配置为更喜欢 IPv4 而不是 IPv6。通过这种方式,您将能够发送电子邮件并访问仅支持 IPv6 的站点。编辑/etc/gai.conf并取消注释以下行:
precedence ::ffff:0:0/96 100
回答by pjammer
If you (or the internet in this case as this question is the first result for this problem) are testing Mailgun, you may get this error if you're using port 25. Change the port to 587worked, even though their docs/quick links says 25is ok to use.
如果您(或在这种情况下是互联网,因为此问题是此问题的第一个结果)正在测试 Mailgun,如果您使用 port ,则可能会收到此错误25。更改端口以587工作,即使他们的文档/快速链接说25可以使用。
回答by pebble8888
I added these to /etc/gai.conf in CentOS7 and it worked.
我将这些添加到 CentOS7 中的 /etc/gai.conf 并且它起作用了。
label ::1/128 0
label ::/0 1
label 2002::/16 2
label ::/96 3
label ::ffff:0:0/96 4
precedence ::1/128 50
precedence ::/0 40
precedence 2002::/16 30
precedence ::/96 20
precedence ::ffff:0:0/96 100
http://blog.asiantuntijakaveri.fi/2014/12/prefer-ipv4-over-ipv6-on-centos-6.html:title
http://blog.asiantuntijakaveri.fi/2014/12/prefer-ipv4-over-ipv6-on-centos-6.html:title
回答by Uma
Try this, if all above fails
试试这个,如果以上都失败了
I got it solved by adding this in application.rb under config
我通过在配置下的 application.rb 中添加它来解决它
require 'net/http' require 'openssl' require 'resolv-replace'

