Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Devise email with Heroku and Sendgrid in Rails?

I've got a simple Rails 3.2.7 app with Devise added that is deployed to Heroku with Sendgrid added. It works fine on heroku for everything except when it needs to do a password retrieve which requires sending an email. From all the posts i have read i suspect i am somehow setting up the mail parameters incorrectly. Any suggestions are appreciated.

For config/environments/production.rb i added

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

for config/initializers/devise.rb i added

config.mailer_sender = "[email protected]"

and for config/environments.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
}
like image 983
user2284821 Avatar asked Apr 25 '13 19:04

user2284821


People also ask

How add SendGrid to Heroku?

Once you have a Heroku app, you can navigate to the Heroku Add-ons page and search for “Twilio SendGrid”. The search will find both the Twilio SendGrid and Twilio SendGrid Marketing Campaigns add-ons. Select Twilio SendGrid. The setup process for Twilio SendGrid Marketing Campaigns is the same.

What is SendGrid used for?

SendGrid is a cloud-based SMTP provider that allows you to send email without having to maintain email servers. SendGrid manages all of the technical details, from scaling the infrastructure to ISP outreach and reputation monitoring to whitelist services and real time analytics.


1 Answers

So, your problem is you were referencing the wrong environment variables. Heroku stores your SendGrid credentials in ENV['SENDGRID_USERNAME'] and ENV['SENDGRID_PASSWORD']. You were using your actual username and password as the 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
}
like image 177
Swift Avatar answered Dec 10 '22 21:12

Swift