16

im having a problem getting sendgrid to send emails successfully on a rails 3.1 app that's using authlogic for authentication and is being deployed on heroku. i have the following action mailer configuration on config/environments/[development.rb and production.rb]:

config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.default_charset = "utf-8"
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
  :address => 'smtp.sendgrid.net',
  :port => 587,
  :domain => ENV['SENDGRID_DOMAIN'],
  :user_name =>  ENV['SENDGRID_USERNAME'],
  :password => ENV['SENDGRID_PASSWORD'],
  :authentication => 'plain',
  :enable_starttls_auto => true
}

for production.rb, the above code is the same except for


    config.action_mailer.default_url_options = { :host => [app name in heroku] }

when i run it development mode, i get the following error reported:


    Completed 500 Internal Server Error in 21740ms
    Net::SMTPFatalError (550 Cannot receive from specified address notification@[app-domain]: Unauthenticated senders not allowed
):

i now dont really know how to set it up to get it working. does anyone with some prior experience on setting up sendgrid on heroku and rails know what's going on?

thank you so much. you guys are the best!!!

Ringo Blancke
  • 2,444
  • 6
  • 30
  • 54

3 Answers3

44

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.

James F
  • 916
  • 2
  • 11
  • 19
  • I don't know why but I had to configure in mail.rb. staging.rb did not work for me. I'm running Rails 3.1 + Cedar stack on Heroku + Devise 2.0 + sendgrid. – Junichi Ito Mar 28 '12 at 23:01
  • 1
    Sorry, that was my mistake. That was a configuration problem. RACK_ENV and RAILS_ENV in Heroku were "production", but I edited config/environments/staging.rb. – Junichi Ito Mar 30 '12 at 20:21
  • First off don't use the password and username directly. Use env variables, that's what they're for. Second: An initializer or in the env file doesn't matter. Environment files load, eventually, the initializer files. The naming doesn't matter either. – krainboltgreene Jul 12 '12 at 00:38
  • 1
    I also had this problem, thanks to Heroku's docs. However, you can keep your SMTP settings in `config/initializers/mail.rb`, as long as you don't put `ActionMailer::Base.delivery_method = :smtp` in `mail.rb`. – jmdeldin Jul 13 '12 at 20:46
  • 1
    And don't forget to actually set the environment vars on Heroku like I did. heroku config:add SENDGRID_USERNAME=foo and heroku config:add SENDGRID_PASSWORD=bar – seanmrafferty Nov 01 '12 at 15:45
  • I had set the `smtp_settings` in `production.rb`, but was testing in `staging` (`RAILS_ENV`). So once I copied the setting to `staging.rb`, the mail sent. Ugh, my bad! – user664833 May 26 '13 at 06:27
  • https://devcenter.heroku.com/articles/sendgrid it say heroku.com for the domain field, should this be the domain of the application or heroku? – Harry Moreno Jun 19 '13 at 00:16
  • the mailer works for me on staging, where the smtp settings are in `config/initializers/mail.rb` but the same does not work in the production environment!! Not sure why – nish Nov 14 '13 at 13:49
0

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.

Alextoul
  • 839
  • 3
  • 9
  • 19
0

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.

Neil Middleton
  • 22,105
  • 18
  • 80
  • 134
  • well i commented out the smtp_settings in production.rb and i got a connection refused error according to heroku logs. Errno::ECONNREFUSED (Connection refused - connect(2)): should i include sendgrid as a gem in the Gemfile? – Ringo Blancke Jan 08 '12 at 23:39
  • No, you should just need to add the add-on to the application and then carry out a deploy. – Neil Middleton Jan 09 '12 at 09:10
  • 2
    @NeilMiddleton The Heroku docs do state that you should add the smtp_settings on a Cedar stack. You should be fine to leave it out though if it's Aspen or Bamboo. [Sendgrid Usage on Heroku](http://devcenter.heroku.com/articles/sendgrid#usage) – Pete Jan 17 '12 at 21:28