Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
564 views
in Technique[技术] by (71.8m points)

smtp - Rails Mailer "Net::OpenTimeout: execution expired" Exception on production server only

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, do a direct connection with 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.

If you get this error:

telnet: Unable to connect to remote host: Connection timed out

then, the problem isn't in 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".


If you get a timeout on telnet, check this:

  1. 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.
  2. port: 587 is obsolete. Major providers are now using 2525 instead. Major cloud services like DigitalOcean, block outgoing connections to 587 as 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.
  3. ipv6 vs ipv4: check if the hostname is being translated as a IPv4. If not, try to disable IPv6 (see others answers).
  4. 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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...