This post originally appeared on the (now retired) Versa Engineering blog
Update: very similar email preview functionality has been integrated into Rails 4.1
Here in the Versa Nerdery, we send HTML emails. We also build our mailers via Test Driven Development (TDD).
One way we’ve tried to limit the effects of context switching between building email logic and implementing the email design (we are full stack engineers, so we do both) is to use our tests to build easily accessible previews of our HTML emails.
Here is a piece of code we have in our
Then, in our mailer test we call this method just before we leave the test, passing in the name of the mailer action we are testing and the message body generated by the test.
Helpful blog post: How to test mailers in Rails 3 / 4 with Rspec
When this spec runs, it will generate an HTML file in the
public/emails/ folder. This means that to preview the email you just need to point your browser to
your_app.url/emails/welcome_to_the_platform.html and see the email in all its HTML goodness!
As you implement the email’s design you can get an updated preview by just running your spec. If you’ve got automated specs (using something like guard), you don’t need to do anything; the specs will build your preview automatically - just hit refresh.
This simple solution is not perfect for all cases, but it has sped up our development process by providing a flow for building HTML emails that mimics our flow for building HTML pagIf you want to preview emails in your development environment when they are sent, you can use Ryan Bates’ Letter Openner - it seems better for testing emailing as part of a larger flow of an app. There is also the fake SMTP server, Mailtrap, but again, it seems less suited to preview emails while implementing design.