Mailers in Rails

John Souza
2 min readOct 19, 2020

This week I studied mailers and how they can be set up in the Ruby on Rails ecosystem. The examples below are the fruits of my study and not completely original to me.

Mailers in rails require at least three components: a mailer inheriting from ApplicationMailer, mailer views, and mailer integration in the desired controller.

Mailer

The mailer will handle all of the logic required for the specific mailer actions. You can have different mailers for different tasks. In Rails, the mailer model you create will inherit from the ApplicationMailer class, which will provide a host of helpful methods.

class UserMailer < ApplicationMailer
default from: 'notifications@example.come'
def welcome_email
@user = params[:user]
@url = 'http://example.com/login'
mail(to: @user.email, subject: 'Welcome to my awesome site')
end

end

Views

The views of a mailer should be done in two formats. Both an HTML version and a plaintext version should be made. Some e-mail clients do not allow HTML to be shown, which makes the plaintext version necessary. These will be stored as ERB files to allow for string interpolation and conditional logic.

<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Welcome to example.com, <%= @user.name %></h1>
<p> You have successfully signed up to example.com, your username is: <%= @user.login %>.<br>
</p>
<p>
To login to the site, just follow this link: <%= @url %>. </p>
<p>Thanks for joining and have a great day!</p>
</body>
</html>
Welcome to example.com, <%= @user.name %>
===============================================
You have successfully signed up to example.com,
your username is: <%= @user.login %>.
To login to the site, just follow this link: <%= @url %>.
Thanks for joining and have a great day!

Controller

Last, you’ll want to incorporate the mailer you created into your controller. The deliver_later action will fire off an asynchronous task to send the email. The rails documentation does recommend, however, that you use a third party gem to make better use of asynchronous messages. This example is a bit messy and could better abstract the logic into the model to make the controller cleaner, but serves as a good example of what exactly will need to be done in the controller.

class UsersController < ApplicationController
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
UserMailer.with(user: @user).welcome_email.deliver_later
format.html {redirect_to(@user, notice: 'User was successfully created')}
format.json {render json: @user, status: :created, location: @user}
else
format.html {render action: 'new'}
format.json {render json: @user.errors, status: :unprocessable_entity}
end
end
end
end

Sources

https://guides.rubyonrails.org/action_mailer_basics.html

--

--