breaking into the unknown…

devise_confirmable : email not send and Net::SMTPAuthenticationError problem


Devise is a great gem to manage user authentication. You can also implement user confirmation using this i,e a confirmation email will be send when a user registered and he will we able to login only after clicking to that email. By default devise_confirmable module is inactive for devise but you can activate it with few changes. Devise provide different support for user confirmation like triggering email, validating confirmation etc. The detail list of methods is documented here. I will just list here the changes which will make you existing devise authentication to support devise_confirmable. I am assuming that the model you are authenticating with devise is User. I will underline any modification we made to existing code

STEP 1: Add filed required for  devise_confirmable  to user model

If you see Your user migration, you will find different devise introduced fields. many of the field which is not needed by default is commented out. Below is the default migration generated by devise for user

class DeviseCreateUsers < ActiveRecord::Migration   
    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
      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 # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at

      ## Token authenticatable
      # t.string :authentication_token


    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
    # add_index :users, :authentication_token, :unique => true

So you can see that field needed for Confirmable, Lockable and Token authenticatable module of devise is commented out as they are not the default. whenever you want to activate any module you need to add those fields to the users table. You can see that for authenticable module we need 4 more  field and then add index to that. So we will define a new migration and add the required field

class AddAuthenticationTokenToUser < ActiveRecord::Migration   
  class AddConfirmableToUsers < ActiveRecord::Migration   
    # Note: You can't use change, as User.update_all will fail in the down migration   
    def up     
      add_column :users, :confirmation_token, :string     
      add_column :users, :confirmed_at, :datetime     
      add_column :users, :confirmation_sent_at, :datetime     
      add_column :users, :unconfirmed_email, :string # add it Only if are using reconfirmable 

      add_index :users, :confirmation_token, :unique => true

    def down
      remove_column :users, :confirmation_token, :confirmed_at, :confirmation_sent_at
      # remove_column :users, :unconfirmed_email # Only if using reconfirmable

run the migration to introduce the newly added filed rake db:migrate

STEP 2: Modifying the model

In model we will do below modification

class User < ActiveRecord::Base

  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable,
    :validatable, :timeoutable, :omniauthable , :token_authenticatable, :confirmable 
    # to use devise_confirmable module, registerable and confirmable module must be there

STEP 3: Modifying the config/intializers/devise.rb

devise confirmable module implement reconfirmable by default i,e if a user has not clicked the confirmation link and try to login then it will send the confirmation link again, if you do not want this, you can deactivate it by setting false in config/intializers/devise.rb as below

config.reconfirmable = false

STEP  4: Setting up smtp server

The mailer setting is written in individual environment file. For example, for development we write it in config/environments/development.rb. My file look like this(Iam showing only mailer code)

config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"

config.action_mailer.smtp_settings = {
address: "", #this is you remote mail server, if you do not specify it rails will use 
mail server installed in your localhost
port: 587, # the port at which mail server is running, for local host it is at 25
domain: "", # just giving a domain name to you smtp server, you can use any name
authentication: "plain", # If your mail server requires authentication, you need to specify 
the authentication type here.This is a symbol and one of :plain, :login, :cram_md5.
enable_starttls_auto: true,
user_name: "",
password:  "test123"

STEP 5: See it working.

restart the server, go to signup page and register a user. Strange, your user did’nt get any email at say with which he has registered. But when you see the console, you do see that email is getting delivered.

Problem 1: confirmation email not really getting delivered to user although he can see it triggered in terminal log.

Solution : This is happening because, you are running your app in development mode for which mail delivery is disabled by default . In your setting in step 4, you should change the value of the below line to true
config.action_mailer.perform_deliveries = false # it is preventing real delivery of the email
config.action_mailer.perform_deliveries = true # now your email will get delivered

Restart the server and try to register a user again.. Now If you are using setting of step4 ,you will encounter the next error

Problem 2: Net::SMTPAuthenticationError in Devise::ConfirmationsController

Solution : The problem here is that devise do authentication of the user you have passed in setting of step 4 i,e the username and password you have passed in step 4 should be a real gmail user. Note that you have set dummy value like user_name: “”, password:  “test123”. This user_name and password should be replaced with
a real gmail account detail. I put user_name: and password: mypassword and it start working🙂

So, Now when a user register himself, he will get a confirmation email, when he will click the email link he will able to login otherwise get message, “You have not activated your account” but the problem here is that, all the user existed earlier will not able to login as at the time of there creation. confirmation module is not activated so they, did’nt get the email. But now since devise_confirmable module is active those user will not bale to login. We need to fix this. We will write a rake task for this in next step

STEP 6 : writing rake task to update existing user as confirmed

Create a user.rake file in lib/tasks folder of your app and below line to it

namespace :users do
  desc "set all old user as confirmed by devise confirmable module"
  task :confirm_existing_user => :environment do
    User.update_all(:confirmed_at =>

Now go to condole and run below command

$ rake users:confirm_existing_user # it will update all user as confirmed

Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

2 thoughts on “devise_confirmable : email not send and Net::SMTPAuthenticationError problem

  1. it is working fine ,but when i try to send a confirmation mail it shows this error , kindly help, and by the way thanks for your post. really appreciated.good work.. i have searched 3 days in google for the best post..

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s