Search code examples
ruby-on-railsemaildevisedevise-confirmable

Devise and confirmation emails


I am using Devise in my Rails app and am sending out emails for email confirmation, password reset, and password change advisory.

  • I would like to know how I would pass in a different image into each of these emails so that I don't need to pass a load of HTML to the layout. Ideally, I would like to pass the image over to the layout if that is at all possible, maybe I need to do this in the controller?

  • How would I go about sending out a different email if a user is only updating their existing email? Currently, devise sends the same confirmation email.

  • Finally, how would I go about sending a welcome email once they have initially confirmed their account, and not if they are only updating their email?

All help would be much appreciated, thanks


Solution

  • You can override devise mailer to meet your requirements.

    First of all create DeviseMailer

    # app/mailers/devise/mailer.eb
    if defined?(ActionMailer)
      class Devise::Mailer < Devise.parent_mailer.constantize
        include Devise::Mailers::Helpers
    
        def confirmation_instructions(record, token, opts = {})
          @token = token
          if record.pending_reconfirmation?
            devise_mail(record, :reconfirmation_instructions, opts)
          else
            devise_mail(record, :confirmation_instructions, opts)
          end
        end
    
        def reset_password_instructions(record, token, opts = {})
          @token = token
          devise_mail(record, :reset_password_instructions, opts)
        end
    
        def unlock_instructions(record, token, opts = {})
          @token = token
          devise_mail(record, :unlock_instructions, opts)
        end
      end
    end
    

    Then just create needed views in app/views/devise/mailer/ for each of methods:

    • reconfirmation_instructions, will be called when user changes they email
    • confirmation_instructions, will be called when user confirms they email
    • unlock_instructions, when account is locked
    • reset_instructions, on password reset

    Actually you can create any template you'd like.

    Hope that will help.