ruby-on-railsdeviseactionmailerdevise-confirmable

Devise sending welcome email after confirmation


I'm using rails 5.2 and devise 4.4.3, I have confirmable working, however, I'm trying to have it so when the user confirms their account they get sent another email welcoming them to the website.

On sign up the confirmation email sends and confirmation can take place by clicking the link in the email but the welcome email that I want to be sent after this currently doesn't send.

The code I have is like so, I have generated a UserMailer and done the following

user.rb model

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

  has_one_attached :avatar

  def confirm!
    welcome_email
    super
  end

  protected

  def welcome_email
    UserMailer.welcome_email(self).deliver
  end
end

views/user_mailer/welcome_email.html.erb

<h2>Welcome <%= @user.email %></h2>

mailers/user_mailer.rb

class UserMailer < ApplicationMailer
  def welcome_email(user)
    @user = user
    mail(to: @user.email, subject: "Welcome! You are awesome!")
  end
end

Solution

  • You can use the after_confirmation callback instead of trying to override confirm.

    def after_confirmation
      UserMailer.welcome_email(self).deliver
    end