Gem of the Week: Letter Opener
August 12, 2016 Jamey Hampton 0 Comments
Not every gem has to completely revolutionize your application. Often, the beauty of using gems is having an easy to use, ready made solution for a small problem that’s making your life as a programmer more difficult. Here’s an example: You’re using ActionMailer in your app to send very nicely styled emails out to your users. You waste a bunch of time configuring ActionMailer in your development environment, which can be tricky. You clog up your own inbox with two dozen test emails while you try to tweak the HTML. You accidentally send a test email to one of your coworkers, or worse, one of your clients.
Sure, it’s possible to do it the old fashioned way, but why not use a gem to avoid all the hassle? Allow me to introduce Letter Opener, a super simple gem for email previewing, written by Ryan Bates, the voice behind Railscasts. Getting Letter Opener set up couldn’t be easier. Slap that bad boy into your Gemfile like this.
gem "letter_opener", :group => :development
Letter Opener is only for development, so it’s helpful to specify the group. If you already have group blocks set up in your Gemfile, just add it into the development block.
Then, instead of configuring your ActionMailer like you normally would in config/environments/development.rb, just use this simple line of code.
config.action_mailer.delivery_method = :letter_opener
Congratulations, you’re all set up! Literally, that’s it. Now when you trigger any emails in development, instead of attempting to send the mail, it will pop up a new browser window, showing you the text and styling in your email. You can audit your HTML styling, you can click on the links to test your email flow, you can do anything you’d normally do with your test email without actually worrying about it being emailed. Plain and rich versions of your email are also saved in the tmp/letter_opener folder for sharing and later comparisons.
Gems allow you to get to work without reinventing the wheel and that’s what’s so great about them. Some of the best gems are simple solutions to simple problems that you didn’t even realize were eating up your time and effort until you don’t have to worry about them anymore. Save your energy for real programming and let Letter Opener worry about the little stuff for you.
(This article was originally published on Uptime.)