Phoenix HTML Mailer
Sooner or later you want to send (HTML) emails with Phoenix. Rails has Mailers for this and Phoenix has a guide that describes how to send basic emails.
I wanted a system that you can use like a Controller or Model the Phoenix way, so I wrote a bunch of modules that let’s you easily send emails.
The actual sending of the emails will be done via Mailgun. I find Mailgun to be an excellent service and even the free plan will satisfy the requirements of most people.
Since I’m not a fan of long prose let’s dig right into code:
lib/my_app/mailer.ex
This module is the core of the actual mailing process. Since you want to be able to send your mails asynchronously with retries when failing, I added the Exq job queue system. Exq uses Redis for queueing (like Ruby’s Resque) under the hood, so you need to have a Redis system available.
The enforce_atom_keys
function ensures that the passed parameters look the same regardless of using the direct or asynchronous sending method
(if async then the params get serialized and the keys will be strings).
Every Mailer doubles as an Exq Worker (hence the perform
function). So when the module gets async-executed by Exq, the perform function will just call the direct sending mechanism.
The defp send_async(mail) when is_list(mail)
is needed because the parameters can be passed by Map or by List (which again is due to serialization by Exq).
web/web.ex
web/mailer/example_mailer.ex
You can decide per Mailer if you want to send the mail directly or asynchronously but in almost all cases async is the way to go.
web/views/email_view.ex
The View is straight forward. Templates will be placed in web/templates/email/
as regular .html.eex
template files.
web/templates/email/example.html.eex
Now you can use the Mailer anywhere to send emails like so MyApp.ExampleMailer.send("phil@jack.chat", "Philip")
That’s about it.
I always found designing and building HTML emails one of the harder things to do when dealing with this topic. But recently I discovered Mjml which is a very nice tool if you want to keep your sanity while working with HTML email designs.