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.

  1. $ hanami generate mailer welcome
  2. create spec/bookshelf/mailers/welcome_spec.rb
  3. create lib/bookshelf/mailers/welcome.rb
  4. create lib/bookshelf/mailers/templates/welcome.html.erb
  5. create lib/bookshelf/mailers/templates/welcome.txt.erb

Let’s see how a mailer is structured:

  1. # lib/bookshelf/mailers/welcome.rb
  2. module Mailers
  3. class Welcome
  4. include Hanami::Mailer
  5. end
  6. end

All the mailers are available under the Mailers namespace.