« 25 Days of Ruby Gems - Ruby Advent Calendar 2020, December 1st - December 25th

Day 7 - letter_opener Gem - Let Your Browser (Automagically) Open-Up Your Email Letters for Previewing. Stop Sending Emails to Yourself for Testing

Written by swanson Matt Swanson

Contrarian-in-training. Building products. Karl Pilkington is my spirit animal. Hacking on Boring Rails and Slotback Labs.

Have you ever sent yourself a test email?

You’re working on adding an email notification in your app and you need to try it out. Time to send yourself a test email…or two…or fifty… We’ve all done it.

But when you’re writing code for your Mailers, wouldn’t it be great if you could quickly preview the email message and iterate on the content?

Enter letter_opener.

Change how emails are ‘delivered’ in your development environment

Rails supports changing your application configuration based on environment. So, for example, you can change how emails are sent through ActionMailer on a per-environment basis.

The letter_opener gem takes advantage of this by allowing you to switch out how emails are sent. Instead of actually delivering an email notification from your code, the gem will instead open the email in your default browser so that you can preview the contents and inspect the contents (as well as the To, From, Subject, and other options).

While Rails does provide a mechanism to preview emails, letter_opener is much more convenient because you don’t have to write special previews for every email you want to test or manually visit the preview URLs. Any emails you send via ActionMailer will be intercepted and shown in the browser.

Configuring the gem

After installing the gem, simply make the following configuration changes in your config/environments/development.rb:

config.action_mailer.delivery_method = :letter_opener
config.action_mailer.perform_deliveries = true

Restart your application server if it was running and voila! Now you can preview emails in the browser and never resort to sending test emails to your personal account :).

Find Out More

References

Built with Ruby (running Jekyll) on 2021-07-25 15:15:02 +0000 in 0.371 seconds.
Hosted on GitHub Pages. </> Source on GitHub. (0) Dedicated to the public domain.