augnustin
Handle emails properly in phoenix
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 submitted, 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…
Marked As Solved
LostKobrakai
Route helpers also work with %URI{} structs.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








