Rails Mailers
Objectives
Describe in details the concepts behind Action Mailers
Identify situations where emails will need to be sent by your application
Use Action Mailers to implement a password reset system
Getting Started
https://github.com/WDI-SEA/rails-password-reset
Use this starter code in order to have an app with user authentication. We'll be building upon this app in order to add the ability to reset passwords.
What are Action Mailers?
An Action Mailer is a class that allows you to send emails in Rails. They are very similar to controllers, where each mailer is a class with different methods. There are also views, which represent the content of the email.
Let's try creating a mailer and identify some components.
Creating a Mailer
We'll be creating a mailer in order to send users a password reset link.
Note how the generator created layouts, views, test files, and a base class for all mailers. Each mailer class inherits from ApplicationMailer
, and mailer classes have methods that represent different emails.
These mailers can be called from the console, but in development, we need to setup email delivery. Or, we can use a gem called letter_opener
to view emails in the browser. Let's do that for now.
Letter Opener
Install letter_opener
to view emails in the browser. Add the gem to your gem file in the development group.
Configure letter_opener
by going to your configuration and adding this line to your config/environments/development.rb
. While we're at it, we'll also configure the email URLs (which must be done manually).
Now emails should appear in the browser. Right? Right. Try testing it by running rails c
and call the mailer:
Now that we've verified the functionality of letter_opener
and our mailer, let's make some routes and a controller to add password reset functionality.
Create Routes and Controller
In routes.rb
The latter two routes will handle our reset code. Then...
Creating a Reset Password Form
In views/passwords/new.html.erb
Add Columns for a Reset Code and Expiration
Note that Add<Attributes>To<Table Name>
will create the migration we need. All we need is to specify the columns.
Let's also add a method to our User
model to set the password reset functionality. We'll do this by generating a reset code, setting an expiration date, and saving the user.
Update the Password Controller
Make sure that we can find the user and set a reset code if found.
Alter the Password Reset Mailer
Lastly, we need to alter our mailer so it accepts a user and prints out the right information to our mailer view.
Alter the action in app/mailers/user_mailer.rb
Make sure to alter the views as well.
In app/views/user_mailer/password_reset.html.erb
But what about the reset? Now we need an edit form and update action to handle the new password.
In passwords_controller.rb
In views/passwords/edit.html.erb
Sendmail
If you would like to test actually sending mail from your local machine, you can use a binary included with MacOSX/Linux called sendmail
. In order to set up sendmail, you will need to set up some additional configuration in your project.
In your config/environments/
folder, there are three environments files which are used for configuration depending on your current environment (dev/test for your machine or prod for deployment on, say, Heroku).
For now, we'll just edit the development.rb
file. Append the following within the do block:
You can see there are some additional parameters you can change, along with some comments on where the sendmail binary should be located, but the main gist of this config is the first line where we specify the sendmail binary as how our machine will send mail.
Simply calling UserMailer.password_reset.deliver_now
will fire off an email to the address provided! Be sure to configure your from email address in app/mailes/application_mailer.rb
.
Heroku
Sendmail, however, is not present on Heroku and you will need to use some external service for sending mail. More information can be found here.
Additional Notes
If you look in your console when you send an email, you'll notice a couple things. Sending the basic pre-generated email will display something like the following:
Notice how the email has both the plaintext and html sections? This is so the email client itself can determine what to display to the client. So if you edit your email, be sure to edit both the html and plain text file!
Last updated