title: Rails Mailer "Net::OpenTimeout: execution expired" Exception on production server only
body: <p>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. <strong>It was working fine a few days ago, but not anymore.</strong> My web host is OVH.</p>
  
  <p>My SMTP settings:</p>
  
  <pre><code>config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true
  
  ActionMailer::Base.smtp_settings = {
    :address              =&gt; "smtp.gmail.com",
    :port                 =&gt; 587,
    :user_name            =&gt; 'sender@gmail.com',
    :password             =&gt; 'secret',
    :authentication       =&gt; 'plain',
    :enable_starttls_auto =&gt; true
  }
  </code></pre>
  
  <p>Using <code>RAILS_ENV=production rails console</code>:</p>
  
  <pre><code>class MyMailer &lt; ActionMailer::Base
    def test_email
      sender     = "sender@gmail.com"
      receiver   = "receiver@example.com"
      mail from: sender, to: receiver, subject: "Hello!", body: "World!!"
    end
  end
   =&gt; nil
  
  MyMailer.test_email.deliver
  </code></pre>
  
  <p>The output:</p>
  
  <pre><code>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 `&lt;top (required)&gt;'
      from script/rails:6:in `require'
      from script/rails:6:in `&lt;main&gt;'2.0.0p0 :029 &gt;
  </code></pre>
  
  <p>I tried the following:</p>
  
  <ul>
  <li>The <code>exception_notification</code> gem was added to the setup a few days ago. I tried to comment its line in <code>Gemfile</code> as well as its matching configuration, and run <code>bundle install</code>. After restarting the server, the issue is still present, even if I delete and recreate the gemset.</li>
  <li>Test it out on a virtual machine (exact same setup as the VPS including iptables rules): <strong>works</strong></li>
  <li>Disable iptables rules: <strong>does not work</strong></li>
  <li>Manually connect to Gmail from the VPS using openssl: <strong>works</strong> (so this is not a firewall issue - see here: <a href="https://stackoverflow.com/questions/1516754/connecting-to-smtp-gmail-com-via-command-line">Connecting to smtp.gmail.com via command line</a>);</li>
  <li>Enable IMAP in Gmail account options (it was disabled): <strong>does not work</strong></li>
  <li>Use a different Gmail account: <strong>does not works</strong></li>
  <li>Replace Ruby 2.0.0 by Ruby 1.9.3</li>
  <li>Upgrade to Rails 3.2.13</li>
  </ul>
  
  <p>Does someone have a possible clue as to how to resolve this issue?</p>
  
  <p>Thanks!</p>