Showing All Headers in Action Mailer Previews

ActionMailer previews can now display all email headers. This allows developers to check all headers, when testing locally.

What are email headers used for?

Headers provide additional information about the email message beyond what is contained in the body of the message. Here are some example use cases for email headers:

Authentication: Email headers can be used to verify the sender’s identity and to help prevent email spoofing and phishing attacks. Some email headers, such as SPF, DKIM, and DMARC, are used to verify that the email message is actually coming from the claimed sender.

Message routing: Email headers can be used to route email messages to specific mailboxes or to trigger specific actions based on the content of the message. For example, the X-BeenThere header is used by mailing list software to track which email addresses are subscribed to the list.

Tracking opens and clicks: Email marketing tools often use email headers to track when a recipient opens an email or clicks on a link within the email. These headers typically include a unique identifier that is associated with the recipient’s email address.

Language and character encoding: Email headers can be used to specify the language and character encoding used in the message content. This can be useful for ensuring that the message is displayed correctly on the recipient’s device.

ActionMailer previews

ActionMailer previews allows developers to preview and test emails. Developers can view and interact with emails in the browser without actually sending them to recipients.

To generate a preview we need to add a preview class to our application, in test/mailers/previews/. Let’s say we want to add a preview for OrderMailer.send_order_confirmation_email, we’d create the following:

1
2
3
4
5
class OrderMailerPreview < ActionMailer::Preview
  def send_order_confirmation_email
    OrderMailer.send_order_confirmation_email(order: order)
  end
end

We can then visualise the email, on localhost, via http://localhost:3000/rails/mailers/order_mailer/send_order_confirmation_email

Viewing all headers

With this new Rails feature, we’ll see a dropdown for viewing all email headers:

actionmailer-previews-all-headers.png