Email confirmation for signup process in a Rails app

In this short article, you’ll learn how to set up a confirmation email for the signup process in a Rails app. Happy learning!

We’re going to use the Mandrill API to send emails through our Rails app.

  1. First, create an account with Mandrill (or log in to your existing account if you already have one).
  2. Go to Settings and open the SMTP & API info. Click on Add API key. Mandrill just created a new API key, that we will use later on.
  3. In your project’s folder, open the config/development.rb file and set config.action_mailer.raise_delivery_errors = true.
  4. In the same file, type :
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.mandrillapp.com',
port: 587,
enable_starttls_auto: true,
user_name: 'type your email address here',
password: 'type your Mandrill API key here',
authentication: 'login'
}

5. Now open the file config/initializers/devise.rb and change :

config.mailer_sender = 'please-change-em-at-config-initializers-devise@example.com'

to

config.mailer_sender = 'Enter the name that you want your users to see when they receive an email (e.g. Lily @ railsGirls <no-reply@railsGirls.com>'

Still in the same file, set :

config.reconfirmable = false

6. In app/model/user.rb, add :confirmable to the list of devise modules.

devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable

7. When we initialy created our Devise object, we didn’t have the confirmable property. we therefore need to add it manually. In your Terminal, type :

rails g migration AddConfirmableToDevise

8. In your project folder, open db/migrate/xxxxx_add_confirmable_to_devise.rb. Because we want to add several properties to our object, we will made the changes in the migration file directly.

def up
add_column :users, :confirmation_token, :string
add_column :users, :confirmed_at, :datetime
add_column :users, :confirmation_sent_at, :datetime
   add_index :users, :confirmation_token, unique: true
end
def down
remove_column :users, :confirmation_token, :confirmed_at,
:confirmation_sent_at
end

9. To make the changes in the database, run rake db:migrate in the Terminal. Restart your Rails server and open your browser (localhost:3000). Try to sign up, you should receive a confirmation email. In the email, click on Confirm account.

You can also customize the template of the confirmation email. To do so open the file app/views/devise/mailer/confirmation_instruction.html.erb.

You’re all set !

See you soon & keep learning !

Cheers, Lily