Phoenix.Template.UndefinedError when finding email template

Hey all, I am working on getting email authentication set up, pretty much following Passwordless Auth. Everything seems to be working great until it gets to the point where it actually grabs the template and sends the email, whence I’m getting the error:

(Phoenix.Template.UndefinedError) Could not render "magic.text" for 
MyApp.EmailView, please define a matching clause for render/2 or define a template at 
"web/templates/email". No templates were compiled for this module.

Here is my emailing module:

# web/emails/authentication_email.ex
defmodule MyApp.AuthenticationEmail do
  @moduledoc """
    Responsible for sending out our verification emails
  """
  use Bamboo.Phoenix, view: MyApp.EmailView

  import Bamboo.Email

  @doc """
    The sign in email containing the magic link.
  """
  def magic_link(token_value, user) do
    new_email()
    |> to(user.email)
    |> from("info@MyApp.com")
    |> subject("Your magic link")
    |> assign(:token, token_value)
    |> render("magic.text")
  end
end

Here is my view:

defmodule MyApp.EmailView do
  use MyApp.Web, :view
end

And my template, which is located at web/templates/email/magic.text

Here is your login link:

<%= signin_url(MyApp.Endpoint, :show, @token) %>

It is valid for one hour.

Everything here looks good to me, and the error and trace indicate it’s looking where the template is, so I am kind of out of ideas since the template is there… Any ideas?

You are defining a text file, not a template.

Just rename it from login_link.text to login_link.text.eex and you are good to go. :slight_smile:

Sorry, the name is magic.text, I copied the wrong thing (although it doesn’t really matter here, neither file name worked). But if you try to put the .eex I get a Bamboo error:

Template name must end in either ".html" or ".text". Template name was magic.text.eex
If you would like to render both and html and text template,
use an atom without an extension instead.

Ooooh, sorry, I see. I didn’t understand how people meant.

For the future, it should be in your emailer module (AuthenticationEmail above)

|> render("magic.text")

but then name your template “magic.text.eex”

I took the errors as contradicting each other, i.e. I thought Bamboo meant file name had to be “magic.text” and then when the file was named like that, Phoenix could not find it because it was missing “.eex”. Thanks!

1 Like