V1.3: Overview


A mailer is an object that’s responsible to deliver a mail message, by rendering one or more templates.

For simplicity, each mailer can handle only one use case (feature). If in our application we need to send emails for several features like: “confirm your email address” or “forgot password”, we will have Mailers::ConfirmEmailAddress and Mailers::ForgotPassword instead of a generic UserMailer that manages all these use cases.

Example

Hanami ships a generator that creates a mailer, two templates and the test code.

$ hanami generate mailer welcome
    create  spec/bookshelf/mailers/welcome_spec.rb
    create  lib/bookshelf/mailers/welcome.rb
    create  lib/bookshelf/mailers/templates/welcome.html.erb
    create  lib/bookshelf/mailers/templates/welcome.txt.erb

Let’s see how a mailer is structured:

# lib/bookshelf/mailers/welcome.rb
module Mailers
  class Welcome
    include Hanami::Mailer
  end
end

All the mailers are available under the Mailers namespace.