Skip to content

From Woodworker to Student to Developer

Writings of Noel Worden

Tag: email

Custom Devise Password Reset Email

The challenge: be able to send out a custom email blast to all app members that they need to change their password by using the provided link.

The process: 

I started with this link, which ends up being a pretty simple .each loop:

User.all.each do |user|
  user.send_reset_password_instructions
end

That seemed easy enough, until I tried to customize it. The send_reset_password_instructions method is baked into the recoverable.rb file of Devise (see where it lives) and -as is- only sends out the standard Devise Reset Your Password email template, which we are utilizing, so I couldn’t change it.

I started by backtracking what was happening in the send_reset_password_instructions  method and found that there was a token being generated with a set_reset_password_token method. I also looked into the email template saw that there was a token being passed along through the path helper.

I dug around a bit more and found this Stack Overflow post, which confirmed for me that I could use that  set_reset_password_token in some fashion. So I went back to that original Devise .each loop and revised it to look like this:

Member.all.each do |member|
  token = member.set_reset_password_token
  MemberMailer.member_import_password_reset(member, token).deliver
end

…but that got kicked back because the set_reset_password_token method is protected.

I knew I was close, so I kept digging and pinged a Slack channel that always seems to come through in a pinch. Turns out there’s a .send method that will bypass a method’s privacy, so I tweaked my logic to be this:

The solution:

Member.all.each do |member| 
  token = member.send(:set_reset_password_token)
  MemberMailer.member_import_password_reset(member, token).deliver
end

With that loop I am able to send every member of the app a custom email, with a Devise reset token. When linked up properly with the custom email template it will push the member to the standard Devise Change Your Password page we have setup.

Author nwordenPosted on February 1, 2018February 1, 2018Categories TechnicalTags custom email, devise, email, password, rails, reset, ruby, ruby on railsLeave a comment on Custom Devise Password Reset Email

Recent Posts

  • Custom Devise Password Reset Email
  • Updating Deeply Nested Attributes with RSpec
  • The Tail of Truncation
  • Ask Me Anything!
  • My First 8 Weeks as a Junior Engineer
Twitter
Visit Us
Follow
LinkedIn
RSS
Follow by Email
Facebook

Recent Comments

  • devon on Ask Me Anything!
  • alan d on Updating Deeply Nested Attributes with RSpec
  • Alex Peattie on Updating Deeply Nested Attributes with RSpec
  • Steven Fischer on My First 8 Weeks as a Junior Engineer
  • Steven Fischer on My First 8 Weeks as a Junior Engineer

Categories

  • Bloc
  • Technical
From Woodworker to Student to Developer Proudly powered by WordPress