I’ve spent the last fortnight or so writing a Rails LiquidMailer. The Rails docs
are useful if you were to design a mailer with Rails’s built in ActionMailer however
the project I was working on involved using LiquidMailer. So this is what
I’ll cover here.
LiquidMailer is great for building Mailers in instances where you don’t require the files generated
with an ActionMailer. Rails provides you with the MVC structure with ActionMailer whereas LiquidMailer
you create what you need. What you don’t get is the Rails plumbing i.e. the MVC (the views, the application_mailer, the tests)
so you build what you need and plug it into your codebase.
Breaking it down into steps this was the flow:
- Create a Mailer inheriting from LiquidMailer
- Create a spec file in Rails’ spec directory
- Create a view in Rails’ view directory
This is pretty much all you will require. Make sure you have the naming right as always with Rails, things break if the naming is
incorrect or does not follow convention. The part I found difficult to find was testing the sending of emails. Here Rails’
ActionMailer::Preview is quite handy. With LiquidMailer you have to do this manually in the console. It comes with LetterOpener built in
so you will be able to check if the send functionality is there. However unless you are testing from production where the backend is configured
to a email server it won’t actually send the email.
So to do this:
- Run Rails console
- ‘Send’ your email using the command
- This should open up a rich.html page with how your email will appear
Other commands that may be useful:
- To check what the delivery method Rails is using
- To change the delivery_method to smtp use
Remember to call the Mailer in the appropriate file once you’ve finished making it!
comments powered by