简体   繁体   中英

Heroku + Sendgrid = Errno::ECONNREFUSED (Connection refused - connect(2))

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
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM