How to setup a Welcome Email after user confirmation

Using a combo of Ruby on Rails, Devise, Action Mailer and Sidekiq

To send a welcome email after a user signs up, you’ll want to override Devise’s RegistrationsController. Here’s how you can do that:

  1. Create a new file in your controllers directory. The path should be app/controllers/users/registrations_controller.rb.

In this file, add the following code:

class Users::RegistrationsController < Devise::RegistrationsController
  def create
    super do |resource|
      if resource.persisted?
        UserMailer.with(user: resource).welcome_email.deliver_later
      end
    end
  end
end

The super keyword calls the original create method from Devise::RegistrationsController, and the block (do |resource| … end) is executed after the user is saved. The resource.persisted? check ensures that the user was successfully saved before sending the email.

  1. Now you need to tell Devise to use your new controller. In your routes.rb file, modify the Devise routes to look like this:
devise_for :users, controllers: { registrations: 'users/registrations' }
  1. Finally, make sure you have a UserMailer with a welcome_email method and a corresponding view. The mailer might look something like this:
class UserMailer < ApplicationMailer
  def welcome_email
    @user = params[:user]
    mail(to: @user.email, subject: 'Welcome to Our Website!')
  end
end

And then you would create a view file at app/views/user_mailer/welcome_email.html.erb (or .text.erb for a text-only email) that contains the body of the email.

This way, you’re working with Devise rather than trying to replace its functionality, and you’re adding your custom welcome email functionality in a way that’s consistent with the design and philosophy of Devise.

To send a welcome email after a user confirms their account:

Devise includes confirmable functionality that requires users to confirm their account before they can sign in. If you have this module enabled, then a user isn’t technically “active” until they have confirmed their account.

To send a welcome email after a user confirms their account, you should override the confirmations_controller.rb instead of the registrations_controller.rb.

Here’s how you can do it:

  1. Create a new file in your controllers directory. The path should be app/controllers/users/confirmations_controller.rb.

In this file, add the following code:

class Users::ConfirmationsController < Devise::ConfirmationsController
  def show
    super do |resource|
      if resource.errors.empty?
        UserMailer.with(user: resource).welcome_email.deliver_later
      end
    end
  end
end

The super keyword calls the original show method from Devise::ConfirmationsController, and the block (do |resource| … end) is executed after the user is confirmed. The resource.errors.empty? check ensures that the user was successfully confirmed before sending the email.

  1. Now you need to tell Devise to use your new controller. In your routes.rb file, modify the Devise routes to look like this:
devise_for :users, controllers: { confirmations: 'users/confirmations' }

This will ensure that the welcome email is only sent after a user has successfully confirmed their account.