Setting Up and Using Ruby on Rails’ Basic Mailer Functionality

Adding email functionality to a Rails app is surprisingly easy. It might seem like a daunting task, but there are plenty of good resources to get you started. The two sources from which I am pulling for this post are LaunchSchool’s how-to and GKhalsa’s 8-minute tutorial on YouTube. I am of the opinion that GKhalsa’s video is easier to understand but that LaunchSchool’s docs are more thorough. Feel free to check them out for yourself. In full transparency, I will be walking you through setting up and using the basic mailer following pretty much the same steps as GKhalsa’s tutorial video with a little more explanation here and there.
The first step to using Rails’ mailer functionality is to create it in your project using Rails’ built-in generator. (Be sure to use snake_case for your class name. Rails will use that snake_case for the directory names and will convert to CamelCase for your class.):

Using this generator will create a few files for you:

First, inside of the app/mailers directory, you will see an application_mailer.rb file:

This will serve as your base mailer class from which your other mailer(s) will inherit following the same sort of structure seen with the ApplicationRecord class in your out-of-the-box Rails file skeleton. Here, you should set up the default email account that your app will use to send it’s emails. For basic functionality, you don’t need to add anything else in this file.
There will be another model file created in app/mailers that will have a class of the same name as whatever you named your mailer in your command line generator (again, snake_case for the file and CamelCase for the class, as is Rails’ convention):

This file starts out empty but is where you will eventually put the methods that will execute your emails. Those methods should take in arguments, which will be anything that you will need passed to your emailer class in order to send an email. At minimum, you will need to pass in an email address or an instance of a class that has an email address attribute, but this will solely depend on your app and the information that you will need to email out to users. A very basic emailing method might look like this:

This basic method takes in the argument of a recipient that is passed to it from wherever in your app you call this method. The recipient is then saved as an instance variable (so that it can be passed to the view). Then, Rails’ built-in mail method is called in which a to: argument and a subject: argument are passed. Finally, your method is going to look to your app/views directory for two things: 1) a subdirectory by the name of your emailer class. Luckily, the generator makes this for you:

…and, 2) an html.erb view file that matches the name of your method. (NOTE: The generator does not make this file for you because it doesn’t know what your emailing methods are going to be called or how many you are going to have. So, you have to touch this file (and any others you will need) yourself as I have done below:)

In your config/environments/development.rb file, you will want to put in the following code (pulled straight from LaunchSchool’s docs):

This code is what is going to allow your application to login to an email account (whichever one you have chosen) and send your emails. It relies on two variables, the ENV[‘gmail_username’] and the ENV[‘gmail_password’] variables, which you will want to assign in a safe place. As per GKhalsa’s suggestion, the Figaro gem is a good tool for safely storing your login information. Add gem ‘figaro’ to your gem file and run bundle in your command line so that your app knows about it. Then, run figaro install in your command line. This creates an application.yml file in your config directory in which you can enter your login information set equal to the variables gmail_username and gmail_password (or whatever you want them to be…as long as they match what you put in your config/environments/development.rb file). The values of those variables will automatically be converted to strings, so you don’t have to worry about stringifying them. Also, Figaro adds this application.yml file to your .gitignore file so that it is not pushed up and made public. Thanks, Figaro.
With the basic setup now complete, all that is left to do is test out your emailer. From whatever other part of your application in which you will need to make a call to email a user, you should add the following code:

The first part is your emailer class, whatever you called it. The second part is the name of your emailer method (again, whatever you called it) into which you must pass any instance variables that need to be sent over to the emailer class. And, finally, deliver is a built in Rails method that tells your application that you want that email sent right now. Once this method chain is run, Rails will go into your emailer class, find the appropriate method, assign the variables as you have instructed, and use the mail method (that you called inside of your custom method) to email your user with your assigned subject and whatever body text you included in the views!
I hope that this simple tutorial was helpful. If you would like more information (or for it to be presented in a different way), feel free to check out GKhalsa’s video and LaunchSchool’s docs, both linked at the beginning of this post.
