← Home
rails

Rails Generate mailer

Reference & Command Builder

Published: 2023-05-11
Author: Harrison Broadbent
Type: Reference & Command Builder

Rails Generate Mailer

Rails Generate Mailer (rails g mailer) is the generator for ActionMailer mailers. These are similar to typical Rails controllers, except they render and deliver emails, not webpages.

The Rails mailer generator will create a mailer in app/mailers, plus the views corresponding to the mailer methods you provide.

A freshly generated mailer looks like this -

# app/mailers/example_mailer.rb
class ExampleMailer < ApplicationMailer
  def example
    @greeting = "Hi"
 
    mail to: "to@example.org"
  end
end

Similar to jobs, ActionMailer emails can be sent synchronously or asynchronously, using the #deliver_now and #deliver_later methods respectively.

Command Builder for rails g mailer

rails g mailer 

Mailer Name

Mailer Method(s)

Generator Options

Runtime Options

# Command Options
❯ rails g mailer

Usage:
  rails generate mailer NAME [method method] [options]

Options:
      [--skip-namespace], [--no-skip-namespace]              # Skip namespace (affects only isolated engines)
      [--skip-collision-check], [--no-skip-collision-check]  # Skip collision check
  -e, [--template-engine=NAME]                               # Template engine to be invoked
                                                             # Default: tailwindcss
  -t, [--test-framework=NAME]                                # Test framework to be invoked
                                                             # Default:

Runtime options:
  -f, [--force]                    # Overwrite files that already exist
  -p, [--pretend], [--no-pretend]  # Run but do not make any changes
  -q, [--quiet], [--no-quiet]      # Suppress status output
  -s, [--skip], [--no-skip]        # Skip files that already exist

# Command Description
Description:
============
    Generates a new mailer and its views. Passes the mailer name, either
    CamelCased or under_scored, and an optional list of emails as arguments.

    This generates a mailer class in app/mailers and invokes your template
    engine and test framework generators.

Example:
========
    bin/rails generate mailer Notifications signup forgot_password invoice

    creates a Notifications mailer class, views, and test:
        Mailer:     app/mailers/notifications_mailer.rb
        Views:      app/views/notifications_mailer/signup.text.erb [...]
        Test:       test/mailers/notifications_mailer_test.rb