Rails Mailer “Net::OpenTimeout: execution expired”

2019-01-17 05:58发布

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.

My SMTP settings:

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
}

Using 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

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_notification gem was added to the setup a few days ago. I tried to comment its line in Gemfile as well as its matching configuration, and run bundle 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

Does someone have a possible clue as to how to resolve this issue?

Thanks!

8条回答
啃猪蹄的小仙女
2楼-- · 2019-01-17 06:37

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.

My problem was that i was using a Google server in production, and it blocks ports 25, 465 and 587 on outbound connections.

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.

查看更多
萌系小妹纸
3楼-- · 2019-01-17 06:40

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.conf and uncomment the following line:

precedence ::ffff:0:0/96 100
查看更多
啃猪蹄的小仙女
4楼-- · 2019-01-17 06:48

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:

#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

cat /proc/sys/net/ipv6/conf/all/disable_ipv6

from terminal. Two possible answers: 0 => IPv6 is enabled; 1 => IPv6 disabled.

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

查看更多
趁早两清
5楼-- · 2019-01-17 06:48

I added these to /etc/gai.conf in CentOS7 and it worked.

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

查看更多
放荡不羁爱自由
6楼-- · 2019-01-17 06:52

Try this, if all above fails

I got it solved by adding this in application.rb under config

 require 'net/http'

 require 'openssl'

 require 'resolv-replace'
查看更多
贪生不怕死
7楼-- · 2019-01-17 06:53

The issue was due to an IPv6 misconfiguration on the production server and has now been fixed.

查看更多
登录 后发表回答