Net::OpenTimeout exception when sending mail using Sendgrid from Rails 4 app on Google Compute Engine

2.2k views Asked by At

I have deployed a Rails 4 application using Capistrano to a Google Compute Engine instance running Debian 7. Im using Nginx as the webserver and Passenger as the application server. For sending emails, im using Sendgrid and i have the following in my staging.rb file

config.action_mailer.default_url_options = { host: 'smtp.sendgrid.net', port: 2525 }
config.action_mailer.delivery_method = :smtp

ActionMailer::Base.smtp_settings = {
    :user_name => '<my_sendgrid_username>',
    :password => '<my_sengrid_password>',
    :domain => '<ip address of my instance>',
    :address => 'smtp.sendgrid.net',
    :port => '2525',
    :authentication => 'plain',
    :enable_starttls_auto => true
  }

I have also opened port 2525 on the firewall of my instance using the google cloud console.However, im getting Net::OpenTimeout exception when my rails app tries to send email . Below is a part of the exception trace

Net::OpenTimeout (execution expired):
  /home/jimcgh/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/smtp.rb:541:in `initialize'
  /home/jimcgh/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/smtp.rb:541:in `open'
  /home/jimcgh/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/smtp.rb:541:in `tcp_socket'
  /home/jimcgh/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/smtp.rb:551:in `block in do_start'
  /home/jimcgh/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/timeout.rb:89:in `block in timeout'
  /home/jimcgh/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/timeout.rb:99:in `call'
  /home/jimcgh/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/timeout.rb:99:in `timeout'
  /home/jimcgh/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/smtp.rb:550:in `do_start'
  /home/jimcgh/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/smtp.rb:520:in `start'
  mail (2.6.3) lib/mail/network/delivery_methods/smtp.rb:112:in `deliver!'
  mail (2.6.3) lib/mail/message.rb:2141:in `do_delivery'
  mail (2.6.3) lib/mail/message.rb:236:in `block in deliver'
  actionmailer (4.2.1) lib/action_mailer/base.rb:543:in `block in deliver_mail'
  activesupport (4.2.1) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (4.2.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.2.1) lib/active_support/notifications.rb:164:in `instrument'
  actionmailer (4.2.1) lib/action_mailer/base.rb:541:in `deliver_mail'
  mail (2.6.3) lib/mail/message.rb:236:in `deliver'
  actionmailer (4.2.1) lib/action_mailer/message_delivery.rb:85:in `deliver_now'
  devise (3.5.1) lib/devise/models/authenticatable.rb:170:in `send_devise_notification'
  devise (3.5.1) lib/devise/models/confirmable.rb:114:in `send_confirmation_instructions'
  devise (3.5.1) lib/devise/models/confirmable.rb:170:in `send_on_create_confirmation_instructions'
  activesupport (4.2.1) lib/active_support/callbacks.rb:432:in `block in make_lambda'
  activesupport (4.2.1) lib/active_support/callbacks.rb:228:in `call'
  activesupport (4.2.1) lib/active_support/callbacks.rb:228:in `block in halting_and_conditional'
  activesupport (4.2.1) lib/active_support/callbacks.rb:506:in `call'
  activesupport (4.2.1) lib/active_support/callbacks.rb:506:in `block in call'
  activesupport (4.2.1) lib/active_support/callbacks.rb:506:in `each'
  activesupport (4.2.1) lib/active_support/callbacks.rb:506:in `call'
  activesupport (4.2.1) lib/active_support/callbacks.rb:92:in `_run_callbacks'
  activesupport (4.2.1) lib/active_support/callbacks.rb:776:in `_run_create_callbacks'
  activerecord (4.2.1) lib/active_record/callbacks.rb:306:in `_create_record'
  activerecord (4.2.1) lib/active_record/timestamp.rb:57:in `_create_record'
  activerecord (4.2.1) lib/active_record/persistence.rb:502:in `create_or_update'

I have read the documentation on Google Compute Engine website about Sending Emails, and see that outgoing on port 25 is blocked, hence am using 2525. I have also tried using :2525 and :plain instead of '2525' and 'plain' in my ActionMailer settings in staging.rb. What could be the problem here ?

Please Help Thank You

3

There are 3 answers

1
ppascualv On

You're giving a bad configuration.

config.action_mailer.default_url_options = { host: (it must be your host ie: 'localhost or www.domain.com', port: 3000(in production usually port is not needed')}

ActionMailer::Base.smtp_settings = {
    :address        => 'smtp.sendgrid.net',
    :port           => 587, #port used by smtp not yours
    :user_name      => ENV['SENDGRID_USERNAME'],
    :password       => ENV['SENDGRID_PASSWORD'],
    :enable_starttls_auto => true,
    :authentication => :plain
    :domain => 'domain.com' #your domain
  }
0
Jason On

A gem exists that solves the problem by providing an ActionMailer DeliveryMethod that uses the SendGrid Web API rather than the SMTP API.

0
Jude Calimbas On

add this in your config, base here https://github.com/mikel/mail/issues/639 tls: true