How to send two different emails for devise confirmable and devise reconfirmable?
Asked Answered
A

3

6

Both devise confirmable (email confirmation when user signs up) and reconfirmable (email confirmation when user changes email) modules send the same email template, "confirmation_instructions". How do I get it so that a different email template is used for confirmable?

Acrosstheboard answered 2/4, 2013 at 17:7 Comment(0)
P
9
#Devise Mailer

def confirmation_instructions(record)
  @resource = record
    if @resource.pending_reconfirmation?
      mail(to: @resource.unconfirmed_email, subject: "Confirm new email") do |format|
        format.html { render ... }
      end
    else
      mail(to: @resource.email, subject: "Confirm new account") do |format|
        format.html { render .... }
      end
    end
end
Phalange answered 2/5, 2013 at 16:3 Comment(0)
S
9

You can override options[:template_name] in the #confirmation_instructions method of your mailer.

class AuthMailer < Devise::Mailer
  helper :application
  include Devise::Controllers::UrlHelpers
  default template_path: 'devise/mailer'

  def confirmation_instructions(record, token, options={})
    # Use different e-mail templates for signup e-mail confirmation and for when a user changes e-mail address.
    if record.pending_reconfirmation?
      options[:template_name] = 'reconfirmation_instructions'
    else
      options[:template_name] = 'confirmation_instructions'
    end

    super
  end
end

Also change this line from device.rb

# config.mailer = 'Devise::Mailer'
config.mailer = 'AuthMailer'
Staffer answered 8/2, 2015 at 14:38 Comment(2)
It was a long time ago, for sure, but how to preview both (reconfirmation and confirmation)?Thug
@Thug you can set the unconfirmed_email of your model instance in your preview method: record.update(unconfirmed_email: '[email protected]'). This marks the record as pending_confirmation.Staffer
A
0

Just looked at the docs and there's a send_on_create_notification method that you can override in the model. So just need to override this method so that the confirmation email is not sent and a different one is sent instead. In that email, I'll just have the confirmation link.

Acrosstheboard answered 3/4, 2013 at 2:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.