Applications send e-mail for notifications, confirmations and more. This lesson covers Action Mailer, keeping development and staging from sending real mail, and getting mail delivered to the inbox.
Important
Work on this lesson in
buildermode.
Learning goals
- You can explain how Action Mailer works: mailer classes, text and HTML views, and delivery methods that differ per environment.
- You can implement and test a mailer, including attachments, and preview mails during development.
- You can explain why development and staging must never send real mail, and how to make sure of it, e.g. with an interceptor.
- You can explain, at least roughly, what decides whether a mail reaches the inbox: valid recipients, sender reputation and authentication, content.
- You can explain why we send mails from form models rather than from core model callbacks.
- You know the constraints of HTML e-mail and where to look up what mail clients support.
Resources
Read what's new to you, skim what's familiar, skip what you already master. Stop when you can meet the learning goals.
Your agent can also generate an overview, a tutorial or an explanation for anything here, tailored to what you already know. Just ask.
- π Rails Guide: Action Mailer Basics Show archive.org snapshot β mailers, views, delivery methods, previews Show archive.org snapshot and interceptors Show archive.org snapshot
- π Action Mailer previews β our card
- π Chapter "Task H1: Sending Confirmation Emails" from Agile Web Development with Rails 8 Show archive.org snapshot (in our library β check which edition we have)
- π Valid recipients: Using the Truemail gem to validate e-mail addresses
- π No accidental mail from development and staging: How to write custom email interceptors, check if you have a local mail server listening, and letter_opener Show archive.org snapshot to open mails in the browser instead
- π Reaching the inbox: E-mail deliverability β our card; Kim's slides on sending mail from Linux Show archive.org snapshot go deeper
- βΆοΈ HTML e-mails: talk HTML E-Mails mit MJML Show archive.org snapshot π with slides; MJML Show archive.org snapshot itself; Can I email Show archive.org snapshot for what CSS mail clients support β not required for the exercises
Exercises
- In MovieDB, if you did not implement sending a welcome e-mail, send an e-mail now.
- In MovieDB, whenever someone creates a movie, send a notification to
moviedb-admin@example.com.- Can you attach the movie's poster as a file attachment to the e-mail?
- Can you write a RSpec test for this?
- In the lesson Form Models we said that we don't like to send e-mails in core model callbacks. We would much rather send e-mails from form model callbacks. Why is that?
- Make sure that MovieDB does not send e-mails from your
Moviemodel, but from a form model likeMovie::Form.
- Make sure that MovieDB does not send e-mails from your
Hint
Action Mailer has a delivery method like
:smtpor:sendmailwhich can be different for each environment. For thetestenvironment the delivery method is set to:test, which means that new e-mails are stored as objects in theActionMailer::Base.deliveriesarray. You can inspect this array in your tests.
Discuss with your mentor
With your mentor, discuss the challenges of implementing HTML mails well.