Mailers in Phoenix 1.3 - in /web directory or not?

I’m curious about best practices with regards to mailers and the new Phoenix 1.3 directory structure.

A mailer isn’t strictly ‘web’ related (a non web based system might send out emails), so my sense is to have a lib/my_app/mailers directory and not put mailers in the /web directory. However, rendering HTML mails involves Phoenix’s views and templates, which do live in the /web directory.

Does anyone have suggestions on the cleanest way to do this? “Official” guidance from the core team might be useful in this area.

1 Like

You’re missing, that phoenix view do not need to be dependent on what’s in your web folder. The ones which are there are totally web related, but you can create your own view templates / view modules based on phoenix.views, which live in your mailer context totally separate to your web views.

I’ve created a sample github repo for it (also for my own don’t-create-it-from-scratch-ever-again storage):
https://github.com/LostKobrakai/phoenix-mailer-context/commit/6872acb17df217d1684c084c4d8699d1f697db80
(Please ignore the formatting issues)

Edit:
One could even move the gettext module in the mailer context as well, but I’m not sure about that. Having the RouteHelpers depend on the web context is fine in my mind, because without web context there are no routes and therefore no RouteHelpers :slight_smile:

6 Likes

I created a “Lib/Proj_name/Messages” context because I use mailers to send messages:

  • to admins to alert a new message has arrived to the system;
  • to users in several situations.
    Also, I have some messages that are sent with forms so, messages are a “business concern” that can be applied in several situations so they have their own Context.

This is excellent, thank you!