Manasi Salvi

Writing A Rails LiquidMailer

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:

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:

 YourMailerName.your_view_template_name(any_params_you_may_require).deliver 

Other commands that may be useful:

 LiquidMailer.delivery_method 
 LiquidMailer.delivery_method = :smtp 

Remember to call the Mailer in the appropriate file once you’ve finished making it!


comments powered by Disqus