Hello,
I’m wondering how to handle emails properly in a phoenix application. There doesn’t appear to be a standard way of doing this, which surprises me.
Coming from the Rails world, my first intention is to bind transaction emails to data actions.
Eg. A Model
can be submit
ted, and this action triggers a ModelEmail.submitted(query)
.
But if I want to call Model.submit(model)
in a console where only the Repo
context is Loaded, not the Endpoint
, I get:
** (ArgumentError) argument error
(stdlib) :ets.lookup(MyApp.Endpoint, :__phoenix_url__)
(phoenix) lib/phoenix/config.ex:42: Phoenix.Config.cache/3
(vae) web/router.ex:1: MyApp.Router.Helpers.application_url/4
(vae) web/emails/model_email.ex:83: MyApp.ModelEmail.submit/1
because the email makes the use of the Endpoint
to get absolute URLs in the email: Routes.model_url(Endpoint, :show, model)
So how should this be done?
IMHO, Sending email should be independent from starting the server, as the only necessary thing is the base URL.
Shouldn’t emails be in a separate process, which can be initialized if desired, and which will receive, either a conn
, or an Enpoint
or eventually a string like https://example.com
so that paths like /path
are turned into https://example.com/path
?
What are the best practices here? I guess I’m not the first one to have this issue…