Sending emails from Heroku using SendGrid and Rails

Using : Ruby 1.9.2, Rails 3.1.3 – also using the cedar stack on Heroku
Please note : I’m new to all this Ruby on Rails stuff, I’m posting these articles so if I make mistakes hopefully people will point them out.

I’ve spent around half a day trying to get this to work so I hope by putting up this post a few others might be a bit more productive. I’m pretty sure I over complicated things originally as working from the start again to write this post – things have magically fallen into place and worked first time! The documentation on Heroku is confusing to say the least – one page says to do one thing, another says something different. But as of writing – this is how I got my Rails app running on Heroku to send out emails using the SendGrid addon.

I’m presuming you already have a Rails 3.1.3 application up and running on Heroku. My application simply stores friends birthdays, and I want to get an email notification each time a new friend is added.

1, From within your application directory in Terminal, run the following command:

heroku addons:add sendgrid:starter

2, In textmate, add a new .rb file called mail_setup.rb in config/initializers directory and paste in the following:

ActionMailer::Base.smtp_settings = {
:address => ‘smtp.sendgrid.net’,
:port => ‘587’,
:authentication => :plain,
:user_name => ENV[‘SENDGRID_USERNAME’],
:password => ENV[‘SENDGRID_PASSWORD’],
:domain => ‘heroku.com’
}
ActionMailer::Base.delivery_method = :smtp

3, Now add an ActionMailer to your rails project.

rails g mailer AdminMailer

4, In app/mailers/admin_mailer.rb add the following method:

def user_added
mail(:to => “your@email.com”, :subject => “New friend added”)
end

Obviously change your@email.com to wherever you want to receive the email 🙂

5, Now you can add, commit and push up to heroku

git add .
git commit -m “AdminMailer added”
git push heroku

6, We can now test to see if we can send an email by running console commands remotely on our Heroku app. At the terminal type:

heroku run console

7, And then try:
AdminMailer.user_added().deliver

Hopefully you will get an email delivered to your inbox!

8, Now to add this code so you get an email delivered whenever a friend is added, simply add this to the create method within your friends controller:

AdminMailer.user_added().deliver

This entry was posted in Uncategorized. Bookmark the permalink.

3 Responses to Sending emails from Heroku using SendGrid and Rails

  1. Ed Taupier says:

    I’m having a problem getting html content working in the email body. I’ve tried setting the content_type to text/html as required, but the received email source always shows the content_type as plain. Is there some setting I’m missing in SendGrid or heroku to get this to work?

  2. Tony Z. says:

    Hi, struggling with this myself at the moment. Same basic stack (rails 3.1.0/ruby 1.9.2/heroku cedar/sendgrid). I added the SMTP config stuff to my production.rb file, and I’m not getting any errors, but I’m not getting any emails either. Does it have to be in a mail.rb file in the initializers directory? Where might I look for log messages? Nothing helpful is showing up in `heroku logs`.

    thanks!
    t.

  3. emil tin says:

    also fails silenty for me. no errors, yet no email going out. looking at the sendmail control panel (at the heroku website), the email count is not moving.

Leave a Reply

Your email address will not be published. Required fields are marked *