How to: Action Mailers in Rails Backend

Ajak Cyer
4 min readDec 3, 2020

--

Photo by Lucas George Wendt on Unsplash

Have you ever wanted to send out emails from and app you’ve created?

Well I’m here to show you how! With Rails ActionMailers you can start sending emails to users, in less than 15 minutes! Let me show you how.

We’re going to create an app where a user gets an email from us when they sign up for an account with us.

First thing you want to do is run the rails generator to create your Mailer like so: rails g mailer <name_of_mailer>.

rails g mailer new_user

After this code is ran, two files are created:

  • app/mailers/new_user_mailer.rb
  • app/views/new_user_mailer

The first file created is our mailer controller and the second is our mailer view folder. This is very similar to running a rails g controller, right? No need to be nervous here.

In our application_mailer file there is some starter code written. Here we just change ‘from@example.com’ to an email that you want the user to receive emails from: ‘test@gmail.com’. I recommend google here since that’s what I’ll be using in this example. Though there is documentation available if you decide to use something else. (this MUST be a valid and existing email that you have access to)

// in: app/mailers/application_mailer.rbclass ApplicationMailer < ActionMailer::Base
default from: 'test@gmail.com'
layout 'mailer'
end

In our first new_user_mailer file, let’s write some code!

So inside of the NewUserMailer class we’re going to have a method called welcome_user that takes a parameter of user, which is our newly created user object. I’ve made the user and instance variable so we can have access to this later on in our view file.

In this case, our user has an email attribute that we can access through @user.email this value goes to our key of ‘to: ’ then in our key of ‘subject: ’ we can write whatever subject line we want our newly created user to see.

// in: app/mailers/new_user_mailer.rbclass NewUserMailer < ApplicationMailer
def welcome_user(user)
@user = user
mail(to: @user.email, subject: "Thank you for signing up!")
end
end

Now we go to the actual controller — action that our user is created by. On my app this is found in app/controllers/users_controller in my create action.

I call my NewUserMailer class with my welcome_user(user) method and user the built in method of .deliver to send this an email to my newly created user.

// in app/controllers/users_controller.rbclass UsersController < ApplicationController
def create
@user = User.create(user_params)

if @user.valid?
NewUserMailer.welcome_user(@user).deliver
render json: @user
end
endend

Now we are almost done. We have our functionality set up and ready to go but we also need some information for google to know to send and email using our account info.

LaunchSchool provides a great resource for the how to do this, which I will describe below.

We go to config/environments/development or config/environments/production depending on if you’re deploying your website or not and add the following code to your Rails.application.configure

config.action_mailer.delivery_method = :smtp# SMTP settings for gmailconfig.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => ENV['gmail_username'],
:password => ENV['gmail_password'],
:authentication => "plain",
:enable_starttls_auto => true
}

As you can see we need to store the ENV keys for our gmail username and password somewhere but this MUST be in a secret file if you plan on pushing to github.

I highly recommend figaro to hide your keys.

Once you have that set up, go to your config/application.yml file and enter in your email information in the key value pairs so you can successfully send out an email.

// in config/application.yml
gmail_username: 'test@gmail.com'gmail_password: 'my_secret_gmail_password'

Now when this is done you can successfully send out welcome emails to users who just signed up to our application.

WAIT! Don’t we have something important to say in your email? Maybe a welcome message?

Let’s welcome our user in this easy final step.

Head over to your views. There is a folder that was created when we ran rails g mailer: app/views/new_user_mailer

Let’s add a view file into this folder that has the same name as the action in the Mailer controller (welcome_user).

touch app/views/new_user_mailer/welcome_user.html.erb

In this view file we can write anything we want to be in our email message. We can treat this like a regular view file. Keeping in mind that we have access to our @user instance variable we can get some of our user’s data:

//in app/views/new_user_mailer/welcome_user.html.erbWelcome, <%= @user.name %>! Thank you so much for signing up for the BEST website EVER!

Thanks for reading! Happy Coding!! 📬

--

--

Ajak Cyer

Software Engineering Student at Flatiron NYC. Learning day by day 💻