Mailers
Explore how to use mailers in Ruby on Rails to format and send emails without embedding business logic. Learn the importance of treating mailers as jobs, previewing emails in the browser, styling them using inline CSS, and configuring development tools like MailCatcher. This lesson helps you effectively integrate and test email functionality in Rails applications.
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 ...