As I offten find to
be the case there was a plethora of documentation around using both
Devise and SendGrid. I was even able to find posts about using the
two together. However, as usual, I had to look through multiple
sources in order to get the two working together. This post is my
attempt to put together the shortest set of instructions possible.
First, my starting
point. I have a functioning application which uses Devise for User
signup and signin. I have not yet implemented any form of email
send. I have also not brought the devise views into my code base so
I figured I'd better start by importing it into my code base.
Getting devise user
views into my code required one command.
rails generate devise:views
This created a
folder in my project called views/devise that contained the following
items:
confirmations
mailer
passwords
registrations
sessions
shared
unlocks
Bringing this into
the project gives the added advantage of being able to customize not only the email messages in mailer, but also the
look and feel of all these views for use throughout the application.
My User model
already had devise included and looked like this:
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
Devise already
supports email confirmations so I just needed to add that module as
shown in the comment. So I added :confirmable like so:
devise :database_authenticatable, :registerable, :recoverable,
:rememberable, :trackable, :validatable, :confirmable
Since I had a
working application complete with users, I needed to add columns to
my user table for
confirmable. This is a simple migration.
rails generate migration AddConfirmableToUsers
confirmation_token:string confirmed_at:datetime
confirmation_sent_at:datetime unconfirmed_email:string
Next run the
migration with:
rake db:migrate
That should set me up with Devise. The next thing I need to do is configure my application to work with SendGrid. Most of this information came from the documentation at SendGrid.com.
Step one: Create a free development account at SendGrid.
Step two: Insert the
configuration for send grid mailer into your config/environment file.
ActionMailer::Base.smtp_settings = {
:user_name => 'YourSendGridLogin',
:password => 'YourSendGridPassword',
:domain => 'YourDomain.com',
:address => 'smtp.sendgrid.net',
:port => 587,
:authentication => :plain,
:enable_starttls_auto => true
}
Note: In the future
I (you) may want to have a different configuration for sending mail
from dev/test vs. production. At that time we will remove this block
from config/environment and create different blocks in
config/environments/development etc.
Step three: Make
sure that your server URL is set correctly for devise in your
config/environments/development and config/environments/production
files.
Since I use
Nitrous.io for development my development configuration needed to
point to my Nitrous server like this:
#devise configuration
config.action_mailer.default_url_options = { :host =>
'http://nurl.use6-8.nitrousbox.com' }
Then in production I
set it to point to my main website:
#devise configuration
config.action_mailer.default_url_options = { :host =>
'http://mycompany.com' }
At this point my
users were able to request confirmation emails and click the links to
confirm the accounts.