sajad torkamani

What is Action Mailer?

Action Mailer is Rails’s framework for creating, previewing, and sending emails. It provides an elegant way to create emails using templates and is very similar to how you can use Action Controller to render HTML views using templates.

Action Mailer lets you use the same API to send emails regardless of what email delivery service you’re using (e.g., SendGrid, Mailgun, AWS SES, etc).

Generate mailer class

bin/rails generate mailer User

create  app/mailers/user_mailer.rb
create  app/mailers/application_mailer.rb
invoke  erb
create    app/views/user_mailer
create    app/views/layouts/mailer.text.erb
create    app/views/layouts/mailer.html.erb
invoke  test_unit
create    test/mailers/user_mailer_test.rb
create    test/mailers/previews/user_mailer_preview.rb

Example mailer class

class UserMailer < ApplicationMailer
  default from: ''

  def welcome_email
    @user = params[:user]
    @url  = ''
    mail(to:, subject: 'Welcome to My Awesome Site')

Example mailer views



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

Plain text


Welcome to, <%= %>

You have successfully signed up to,
your username is: <%= @user.login %>.

To login to the site, just follow this link: <%= @url %>.

Thanks for joining and have a great day!

Call the mailer

class UsersController < ApplicationController
  # POST /users
  def create
    @user =

      # Tell the UserMailer to send a welcome email after save
      UserMailer.with(user: @user).welcome_email.deliver_later
      redirect_to(@user, notice: 'User was successfully created.')
      render action: 'new'

deliver_later will enqueue the email as a job with Rail’s Active Job.

Preview emails

ActionMailer previews let you visit a special URL so that you can see how your emails render. Here’s how to preview emails.

Create a base ApplicationMailerPreview

This step is optional, but I find it useful to have a base class that all other preview classes can inherit from. It lets you share common functionality (e.g., including the FactoryBot methods in your preview classes).

# frozen_string_literal: true

class ApplicationMailerPreview < ActionMailer::Preview
  include FactoryBot::Syntax::Methods

Create preview class

For the UserMailer example, you’d create the following UserMailerPreview class at spec/mailers/previews/user_mailer_preview.rb (if using RSpec) or test/mailers/previews/user_mailer_preview.rb (if using Minitest):

class UserMailerPreview < ActionMailer::Preview
  def welcome_email
    UserMailer.with(user: User.first).welcome_email

Note how the method name welcome_email is the same as that inside our UserMailer class.

Preview mailer

Visit http://localhost:3000/rails/mailers and you should see a link to welcome_email. Clicking on this link will render a preview of your email.

Action Mailer configuration options

See docs.


Tagged: Rails