Using Devise and SendGrid to send confirmation email on rails app

  • First make normal rails app with devise user model.

    $ rails new awesome-app This will create new app

    $ cd awesome-app It will change the directory to our new project

  • Setup Gemfile.

    Add gem 'devise' and gem 'sendgrid-ruby' to the Gemfile

    finally run $ bundle install on terminal

  • Setup devise

    $ rails generate devise:install it will create config/initializers/devise.rb and config/locales/devise.en.yml file and follow the instruction generated by above command

    Now generate devise user model with $ rails generate devise User

    then go to app/models/user.rb and make this file look like this:

    class User < ApplicationRecord
      # Include default devise modules. Others available are:
      # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :validatable, :confirmable
    end
    

    Uncomment the Confirmable section of migration file located on the app/db/migrate

class DeviseCreateUsers < ActiveRecord::Migration[7.0]
  def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      # t.integer  :sign_in_count, default: 0, null: false
      # t.datetime :current_sign_in_at
      # t.datetime :last_sign_in_at
      # t.string   :current_sign_in_ip
      # t.string   :last_sign_in_ip

      # Confirmable
      t.string   :confirmation_token
      t.datetime :confirmed_at
      t.datetime :confirmation_sent_at
      t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end

Then run $ rails db:migrate

  • Set up SendGrid

    add gem 'dotenv-rails' on development and test group on gem file and run $ bundle install

    create .env file in your project root and add following line inside it

    SENDGRID_API_KEY = 'YOUR API KEY'

    Then, modify this Devise config to use your SendGrid sender identity:

    # config/initializers/devise.rb
    config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com'
    
  • Set up Action Mailer

    create config/initializers/action_mailer.rb file and add following code

 # config/initializers/action_mailer.rb
ActionMailer::Base.smtp_settings = {
    user_name: "apikey",
    password: ENV['SENDGRID_API_KEY'],
    domain: "your-domain.com",
    address: "smtp.sendgrid.net",
    port: 587,
    authentication: :plain,
    enable_starttls_auto: true
}
  • Now let Test it

    run the server on local $ rails server

go to link localhost:3000/users/sign_up on browser

image.png

After signing up we will get the confirmation email.

image.png