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: 'notifications@example.com'

  def welcome_email
    @user = params[:user]
    @url  = 'http://example.com/login'
    mail(to: @user.email, subject: 'Welcome to My Awesome Site')
  end
end

Example mailer views

HTML

app/views/user_mailer/welcome_email.html.erb:

<!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>

Plain text

app/views/user_mailer/welcome_email.text.erb:

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!

Call the mailer

class UsersController < ApplicationController
  # POST /users
  def create
    @user = User.new(user_params)

    if @user.save
      # 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.')
    else
      render action: 'new'
    end
  end
end

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
end

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
  end
end

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.

Sources

Tagged: Rails

Leave a comment

Your email address will not be published. Required fields are marked *