2

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

Alan
  • 459
  • 6
  • 20

1 Answers1

2

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.

retgoat
  • 2,414
  • 1
  • 16
  • 21
  • 1
    looks like what i need, awesome :D – Alan Jul 27 '16 at 11:43
  • how can i get it to pass in images so my layout can use the image that's passed int? – Alan Jul 27 '16 at 11:43
  • I've also noticed that the password changed email is no longer working? I tried to add in `password_change` but it's not working, any ideas? – Alan Jul 27 '16 at 12:17
  • Could you please explain where exactly you would like to pass an image? – retgoat Jul 27 '16 at 13:45
  • 1
    It's ok i worked it out, i added a variable under the `@token` called `@emailimage`. This allowed me to send the required image into the email layout :D -- Still cant get the password updated email though – Alan Jul 27 '16 at 14:45
  • I'm not sure, but had you checked this advice? https://github.com/plataformatec/devise/wiki/Notify-users-via-email-when-their-passwords-change ? – retgoat Jul 27 '16 at 14:58
  • Actually it's not needed to create additional mailer, you can easily add `password_update` method to existing devise mailer. – retgoat Jul 27 '16 at 15:02
  • I have tried `password_update` and `password_change` neither seem to be working. I will have another look into it shortly. Must be down to my naming of the methods. – Alan Jul 27 '16 at 16:23
  • Not `password_change` but `encrypted_password_changed?` that's an ActiveModel::Dirty http://api.rubyonrails.org/classes/ActiveModel/Dirty.html not devise – retgoat Jul 28 '16 at 01:13