Previewing mailers in Hanami 3.0

Good question! That one is covered in the README :slight_smile:

If you don’t use Hanami View, override the internal rendering methods. These are also a hook for integrations with other rendering systems.

class CustomMailer < Hanami::Mailer
  from "custom@example.com"
  to { |user:| user[:email] }
  subject "Custom Email"

  expose :user

  private

  def render_view(format, input)
    user = input[:user]

    case format
    when :html
      <<~HTML
        <html>
          <body>
            <h1>Hello, #{user[:name]}!</h1>
          </body>
        </html>
      HTML
    when :text
      "Hello, #{user[:name]}!"
    end
  end
end

The dependency on hanami-view is completely optional. If it’s available (i.e. in your bundle), then mailer rendering will use it. You can also disable it explicitly with config.integrate_view = false.

1 Like