397 Rails: Sending e-mail [2d]

Updated . Posted . Visible to the public.

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 builder mode.

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.

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 Movie model, but from a form model like Movie::Form.

Hint

Action Mailer has a delivery method like :smtp or :sendmail which can be different for each environment. For the test environment the delivery method is set to :test, which means that new e-mails are stored as objects in the ActionMailer::Base.deliveries array. You can inspect this array in your tests.

Discuss with your mentor

With your mentor, discuss the challenges of implementing HTML mails well.

Profile picture of Henning Koch
Henning Koch
Last edit
Michael LeimstΓ€dtner
Keywords
email
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra Curriculum (2015-07-08 17:49)