EmmaO91
EmmaO91

Reputation: 458

Sendgrid via Devise: Attempt to send authentication email leads to OpenTimeout error when using Cloud 9 dev environment

I'm relatively new to programming and very new to web development and have been working my way through a Ruby on Rails bootcamp over the past few weeks. The current step I'm on involves adding user authentication to my site using Devise, but I'm getting a "silent error" where the authentication email won't send.

In line with the guide I'm following as part of the bootcamp, I've gone through the following steps:

  1. Installed the Devise gem
  2. Configured it by appending the following to my development.rb file:

    ...
        config.action_mailer.default_url_options = { host: 'localhost:3000' }
        config.action_mailer.delivery_method = :smtp
        config.action_mailer.perform_deliveries = true
        config.action_mailer.raise_delivery_errors = true
    end
    
  3. Created a user model using devise before updating the [timestamp]_devise_create_users.rb file to include the confirmable option.

  4. Ran rake db:migrate

  5. Added the Sendgrid addon (confirmed using "heroku addon")

  6. Created a setup_mail.rb initializer with the following code:

    if Rails.env.development?
        ActionMailer::Base.delivery_method = :smtp
        ActionMailer::Base.smtp_settings = {
            address:        'smtp.sendgrid.net',
            port:           '587',
            authentication: :plain,
            user_name:      ENV['SENDGRID_USERNAME'],
            password:       ENV['SENDGRID_PASSWORD'],
            domain:         'heroku.com',
            enable_starttls_auto: true
        }
    end
    
  7. Installed the Figaro gem

  8. Added SENDGRID_USERNAME & SENDGRID_PASSWORD to application.yml
  9. Set production: SECRET_KEY_BASE to a randomly generated token within secrets.yml

Within Dev mode, the app now successfully loads the user sign-up form, but after signing up an authentication email is not sent. The following error is instead generated:

Net::OpenTimeout in Devise::RegistrationsController#create

I've looked around and it seems a few other people have reported the same error when using Cloud 9 but none have offered suggestions on how to resolve the problem. My (somewhat uneducated) theory is that the host setting in development.rb needs to be different or the port in setup_mail.rb needs to be different as I'm using the C9 service instead of a local machine - but I'm not sure how to find out what these should be set to (if indeed that is the problem).

So far I've tried

My User.rb file currently looks like this

class User < ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable, :confirmable
end

Can anyone give me a clue as to what's going on?

Upvotes: 3

Views: 2607

Answers (1)

Petros Kyriakou
Petros Kyriakou

Reputation: 5343

Change your port to something like 2525, I was getting the same error it seems that cloud9 blocks the port 587.

sample settings for mandrill and cloud9 (should work the same for send-grid aswell)

config.action_mailer.default_url_options = { host: '0.0.0.0:8080' }

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address: 'smtp.mandrillapp.com',
    port: 2525,
    enable_starttls_auto: true,
    user_name: '<your-username>',
    password: '<your-password>',
    authentication: 'login'
  }

Also any changes you make,you must restart the server each time.

Upvotes: 8

Related Questions