How to fix 'render/2 is undefined' error using Bamboo with Phoenix
Asked Answered
R

1

7

I'm trying to send an email using a html email template with Bamboo (and Amazon SES) for my Phoenix/Elixir application

I've managed to get the email sending successfully using Bamboo's |> text_body(message) method. However I now want to be able to send a html template not just a string so I'm trying to use the render fn https://hexdocs.pm/bamboo/1.1.0/Bamboo.Phoenix.html#render/3 but I'm experiencing the following error: function MyApp.HtmlEmailView.render/2 is undefined (module MyApp.HtmlEmailView is not available). I get no error about my view when I use the text_body fn.

This is my fn:

  def send_test_html_email(to_email_address, subject) do
    new_email()
    |> from("[email protected]")
    |> to(to_email_address)
    |> subject(subject)
    |> render("email.html")
  end

and this is the example fn from the docs:

  def html_email do
    new_email
    |> render("html_email.html")
  end

I can't see any difference and as I mentioned before, the rest of the fn was working fine with text_body as the last line instead of render.

My expected result is that the email will send without an error. My actual result is the function MyApp.HtmlEmailView.render/2 is undefined (module MyApp.HtmlEmailView is not available) error.

Ringler answered 26/3, 2019 at 9:22 Comment(2)
Based on that error message, you have no module called MyApp.HtmlEmailView. Can you please double check that there is no typo in the module name, both at the call site and in the definition?Selfemployed
What is the name of module that implements send_test_html_email function, and did you add line use Bamboo.Phoenix, view: MyApp.HtmlEmailView in that module? Also, do you have MyApp.HtmlEmailView module?Clifford
R
2

Thanks Justin Wood and Milan Jaric for your comments.

I was using: use Bamboo.Phoenix, view: MyApp.HtmlEmailView at the top of my module however the error was being caused as view: MyApp.HtmlEmailView needed to be view: MyAppWeb.HtmlEmailView. This has resolved the issue. Thank you for your help.

Ringler answered 27/3, 2019 at 14:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.