Question

I'm trying to get the Heroku Sendgrid add-on working with my Rails app, but keep on getting a connection error:

Errno::ECONNREFUSED (Connection refused - connect(2)):

I've looked at the other SO questions relating to this, but the solution offered- to move the ActionMailer config from environment files to an initializer- didn't fix anything for me.

Here's my current setup:

config/initializers/smtp.rb:

ActionMailer::Base.default_url_options =  { host: 'http://my-app.herokuapp.com/'}
ActionMailer::Base.delivery_method = 'smtp'
ActionMailer::Base.smtp_settings = {
  :user_name => ENV['SENDGRID_USERNAME'],
  :password => ENV['SENDGRID_PASSWORD'],
  :domain => 'heroku.com',
  :address => 'smtp.sendgrid.net',
  :port => 587,
  :authentication => :plain,
  :enable_starttls_auto => true
}
Was it helpful?

Solution

That should work. However, I have added the following lines to /environments/productions.rb to get mine working.

  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.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'
  }
  config.action_mailer.default_url_options = { :host => 'your_domain.com' }

And, make sure to set up your SendGrid add-on correctly for your app on Heroku.

You can check to see if SendGrid is configured properly on Heroku:

$ heroku config

You should see SENDGRID_PASSWORD and SENDGRID_USERNAME.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top