Mailers

Role of a Mailer

A Mailer is a class responsible to send transactional emails such as reporting, password recovery, welcome emails, confirmations, etc.

Location

  • app/mailers/
  • Should be suffix with _mailer.rb

Dos

  • Always send emails asynchronously using a Background Job.
  • Use mailcatcher or real SMTP in integration test
  • All assets displayed in emails should be in public/emails/ because you want to keep track of assets for all emails. If in an email you have a logo with src https://mysite.com/emails/logo.png. And in a new email template you have a new logo, you should use: https://mysite.com/emails/logo-v2.png and keep https://mysite.com/emails/logo.png in place for the old emails.
  • Raise error in development if there is any problem: config.action_mailer.raise_delivery_errors = true
  • Use _url and no _path when generating links because all links should be absolute.

Don'ts

  • Do complexe queries, it's like a controller, it should reuse other classes and only prepare the HTML output.

Code

class UserMailer < ApplicationMailer
  def welcome(user)
    @user = user
    subject = "Welcome #{@user.name}!"

    mail(to: @user.email,
         subject: subject,
         bcc: "[email protected]")
  end
end

results matching ""

    No results matching ""