How to set up email using Heroku and Sendgrid in Rails?

I have a simple Rails 3.2.7 application with the addition of Devise, which is deployed to Heroku with the addition of Sendgrid. He works great for the hero for everything, except when he needs to perform a password recovery that requires sending an email. Of all the messages I read, I suspect that I somehow incorrectly configured the mail settings. Any suggestions are welcome.

For config / environment / production.rb I added

config.action_mailer.default_url_options = { :host => 'smtp.sendgrid.net'} 

for config / initializers / devise.rb i added

 config.mailer_sender = "mail-to-send@from.com" 

and for config / environment.rb I added

 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 } 
+8
heroku devise sendgrid
source share
1 answer

So your problem is that you referenced the wrong environment variables. Heroku stores your SendGrid credentials in ENV['SENDGRID_USERNAME'] and ENV['SENDGRID_PASSWORD'] . You used your actual username and password as key names.

This will work:

 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 } 
+8
source share

All Articles