Ruby on Rails: How to send Devise emails via the Rails console and MailCatcher
Ruby on Rails: How to send Devise emails via the Rails console and MailCatcher
Whether you need to send confirmation instructions, password resets, or other notifications, doing so directly from the console can save time and simplify the process. This article will guide you through that process.
Setup mailcatcher
First, you need to set up the Mailcatcher gem, which will act as your local SMTP server. You can read my tutorial on setting up MailCatcher.
Manually Sending Devise Emails
Now that Mailcatcher is configured, you can manually send different types of Devise emails through the Rails console.
- Start by opening your Rails console:
1
rails console
- Find the user to whom you want to send the email:
1
user = User.last
- Send the desired email
- Confirmation instructions:
1
Devise::Mailer.confirmation_instructions(user, user.instance_variable_get(:@raw_confirmation_token)).deliver_now
- Email changed:
1
Devise::Mailer.email_changed(user).deliver_now
- Password change:
1
Devise::Mailer.password_change(user).deliver_now
- Reset password instructions:
1
Devise::Mailer.reset_password_instructions(user, user.instance_variable_get(:@raw_confirmation_token)).deliver_now
- Unlock instructions:
1
Devise::Mailer.unlock_instructions(user, user.instance_variable_get(:@raw_confirmation_token)).deliver_now
Summary
Mastering the ability to send Devise emails through the Rails console can greatly enhance your development workflow. With these commands at your disposal, you can efficiently test and verify email functionalities locally before deploying them live.
This post is licensed under CC BY-NC 4.0 by the author.