Mailers

Learn to use mailcatcher to configure email sending in Ruby on Rails.

Overview

We want to touch briefly on some other parts of Rails that we had termed boundary classes back in the Rails Application Architecture lesson. Like controllers and jobs, rake tasks are a mechanism for triggering business logic. Mailers, like views, render output for a user. Both rake tasks and Mailers exist at the outside of the app, interacting with the outside world, just as a controller does.

This section of the course will focus on mailers and rake tasks. We’ll mention Mailboxes, Action Cable, and Active Storage only briefly because we have not used these parts of Rails in production. We don’t want to give advice on something we haven’t actually used. Let’s start with mailers.

Mailers are a bit of an unsung hero in Rails apps. Styling and sending email is not an easy thing to do and yet Rails has a good system for handling it. It has an API almost identical to rendering web views, it can handle text and HTML emails, and connecting to any reasonable email provider is possible with a few lines of configuration. And it can all be tested.

There are three things to consider when writing mailers. The first is to understand the purpose of a mailer and thus not put business logic in it. Second, understand that mailers are really jobs, so the arguments they receive must be considered ...