Sendgrid / email sending issues in Ruby on Rails (hosted on Heroku)

独自空忆成欢 提交于 2019-11-28 05:04:24

I spent half a freakin' day on this and finally got mine working now. Quite frustrated as it was due to a poor documentation error. I'm running Rails 3.1 and Cedar stack on Heroku by the way.

So http://devcenter.heroku.com/articles/sendgrid will tell you to put your SMTP settings stuff in config/initializers/mail.rb. BUT... on http://docs.sendgrid.com/documentation/get-started/integrate/examples/rails-example-using-smtp/ it says to put all your SMTP settings stuff in config/environment.rb instead of config/initializers/mail.rb

So the solution is to put that in your environment.rb file. This is how my environment.rb looks:

# Load the rails application
require File.expand_path('../application', __FILE__)

# Initialize the rails application
Freelanceful::Application.initialize!

# Configuration for using SendGrid on Heroku
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
  :user_name => "yourSendGridusernameyougetfromheroku",
  :password => "yourSendGridpasswordyougetfromheroku",
  :domain => "staging.freelanceful.com",
  :address => "smtp.sendgrid.net",
  :port => 587,
  :authentication => :plain,
  :enable_starttls_auto => true
}

To get your SendGrid username and password, type

$ heroku config -long

Hope that helps.. and more people in the future of this headache.

I'm assuming you mean development mode as in locally? If so, I don't think the SendGrid add-on let you send email from outside the Heroku network (as they have standalone accounts that they would prefer you to use).

Saying that, you don't need to configure mail in production when using the SendGrid add-on as it is automagically configured for you when you deploy your application.

Therefore you can remove your config.action_mailer.smtp_settings code and simply use the default in development.

Also note that if you're running your Heroku app on the Bamboo stack you don't need to configure your settings in the environment.rb file since Heroku does it for you.

However you do need to git push at least once after you activated the app to Heroku to set these settings. I made that mistake this morning and found your post.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!