Can't get Swoosh to use the template I'm specifying.

Hi,

I´m trying to send email using Phoenix.Swosh, but I get this error message that I don´t really understand.

This is how I send the mail:

      MyCompany.UserEmail.mail_requesting_signature(%{
        contract: contract,
        current_user: sender
      })
      |> MyCompany.Mailer.deliver()

defmodule MyCompany.UserEmail do
  use Phoenix.Swoosh,
    template_root: "lib/MyCompany_web/templates/emails/",
    template_path: "requesting_signature"


  def mail_requesting_signature(%{
        contract: contract,
        current_user: current_user
      }) do
    sender_email = extract_string(current_user.email)
    recipient_email = contract.client.email
    recipient_name = "John Doe"

    new()
    |> to({recipient_email, recipient_name})
    |> from({"MyCompany", "no-reply@MyCompany.no"})
    |> subject("#{sender_email} has invited you to sign a contract")
    |> render_body(
      "requesting_signature.html.heex",
      %{
        username: user.email,
        title: contract.title,
        content: contract.content,
      }
    )
  end
end
 ** (Phoenix.Template.UndefinedError) Could not render "mail_template.html.heex" for MyCompany.UserEmail, please define a matching clause for render/2 or define a template at "lib/MyCompany_web/templates/emails/mail_template/*". The following templates were compiled:

 * mail_template.html

 Assigns:

 %{title: "Pacocha Group", username: "user.email", email: %Swoosh.Email{subject: "merlin_gibson@rempel.org has invited you to sign a contract", from: {"MyCompany", "no-reply@MyCompany.no"}, to: [{"deion2076@balistreri.biz", "John Doe"}], cc: [], bcc: [], text_body: nil, html_body: nil, attachments: [], reply_to: nil, headers: %{}, private: %{phoenix_view: MyCompany.UserEmail, phoenix_template: "mail_template.html.heex", phoenix_layout: false}, assigns: %{title: "Pacocha Group", layout: false, username: "user.email", content: "Vero repellendus velit in nihil enim voluptatem rerum est et. Voluptate vel aspernatur et accusantium mollitia ea reprehenderit expedita aut. Et debitis veritatis dolore tenetur cumque et ut. Expedita itaque maxime dolorem autem. Perspiciatis eum dolores rerum delectus dolores commodi sunt.", sign_url: "http://localhost:4000/contracts/f46b280d-e05e-418b-805a-079f473db7ae/sign"}, provider_options: %{}}, content: "Vero repellendus velit in nihil enim voluptatem rerum est et. Voluptate vel aspernatur et accusantium mollitia ea reprehenderit expedita aut. Et debitis veritatis dolore tenetur cumque et ut. Expedita itaque maxime dolorem autem. Perspiciatis eum dolores rerum delectus dolores commodi sunt."}

 Assigned keys: [:title, :username, :email, :content,]

The template:
lib/quantik_web/templates/emails/requesting_signature/requesting_signature.html.heex

<div>
  <h1>Document to be signed</h1>
  <h1>Welcome to MyCompany, <%= @username %>!</h1>
  <pre> Please read the document below: </pre>
  <h2><%= @title %></h2>
  <pre> <%= @content %> </pre>
</div>

I think you need to drop the .heex part of the template name you’re passing into Phoenix.Swoosh.render_body/3.

Edit: but I think your template is also not found where phoenix_swoosh expects it, because it’s not being listed as one of the possible templates in the error. It is searching here: lib/MyCompany_web/templates/emails/mail_template/*, where it can find mail_template.html.heex (I guess), but not your other template. Make sure they are next to each other.

(Wrong post…)

Yes, that was it! I removed the .heex part of the template name, and it worked.
Thanks, @linusdm :blush:

patch

     |> render_body(
-      "requesting_signature.html.heex",
+      "requesting_signature.html",
       %{
1 Like