我在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
}
使用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
输出:
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 >
我尝试了以下内容:
exception_notification
gem已添加到设置中。我尝试在Gemfile
及其匹配配置中对其行进行评论,然后运行bundle install
。重新启动服务器后,即使我删除并重新创建gemset,问题仍然存在。是否有人知道如何解决此问题?
谢谢!
答案 0 :(得分:16)
我可能遇到了同样的问题,我的生产应用程序没有发送邮件,尽管开发中的所有内容都运行良好。我也得到了“Net :: OpenTimeout”错误。
我的问题是我在制作中使用了Google服务器and it blocks ports 25, 465 and 587 on outbound connections。
由于我使用Mandrill发送邮件,I was able to switch the connecting port from 587 to 2525 and everything is okay now.
答案 1 :(得分:14)
首先,与Telnet直接连接:
telnet smtp-relay.sendinblue.com 587
Trying 94.143.17.4...
这是基本连接故障排除,适用于任何提供商或端口。用您的实际主机名/端口替换SendBlue和587端口。
如果您收到此错误:
telnet: Unable to connect to remote host: Connection timed out
然后,问题不在Rails中。
在上面的示例中,问题出在端口号中。 sendinblue或mandrill等服务(我相信gmail也不再支持587端口)。 “2525”是新的“587”。
如果您在telnet上超时,请检查:
答案 2 :(得分: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
现在,应用程序可以再次发送电子邮件。
您始终可以知道是否已启用IPv6调用
cat /proc/sys/net/ipv6/conf/all/disable_ipv6
终端两个可能的答案: 0 =&gt; IPv6已启用; 1 =&gt; IPv6已禁用。
来自:https://serverfault.com/questions/512744/timeout-error-in-all-my-apps-for-every-call-to-smtp-servers
答案 3 :(得分:5)
问题是由于生产服务器上的IPv6配置错误,现在已经修复。
答案 4 :(得分:5)
您可以将Ubuntu配置为首选IPv4 over IPv6。这样,您就可以发送电子邮件并访问仅限IPv6的站点。修改@SqlGroup({
@Sql(scripts = "script1.sql", config = @SqlConfig(dataSource = "dataSource1", transactionManager = "txMgr1")),
@Sql(scripts = "script2.sql", config = @SqlConfig(dataSource = "dataSource2", transactionManager = "txMgr2"))
})
并取消注释以下行:
/etc/gai.conf
答案 5 :(得分:0)
如果你(或者在这种情况下因为这个问题的互联网是这个问题的第一个结果)正在测试Mailgun,如果你使用端口25
,你可能会收到此错误。将端口更改为587
工作,即使他们的文档/快速链接显示25
可以使用。
答案 6 :(得分:0)
我将这些添加到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
答案 7 :(得分:0)
如果上述所有操作均失败,请尝试此操作
我通过在config
下的application.rb中添加它来解决它require 'net/http' require 'openssl' require 'resolv-replace'