Hey Folks,
Recently I added email functionality to my phoenix app. I used these two tutorials as reference:
And this doc:
Between all three of these sources there were a few things I feel I have misunderstood and want to enquire about. I’d also like to get some insight into an issue that arose while creating and rendering the HTML email templates.
I used the popular bamboo library to send the emails and the bamboo smpt adapter to send through amazon ses. I adapted the general structure outlined in the first tutorial to my test project.
Project mailer:
// lib/mailer_tutorial/mailer.ex
defmodule MailerTutorial.Mailer do
use Bamboo.Mailer, otp_app: :mailer_tutorial
end
Project email:
// lib/mailer_tutorial/mailer.ex
defmodule MailerTutorial.Email do
use Bamboo.Phoenix, view: MailerTutorialWeb.EmailView
def welcome_text_email(email_address) do
new_email()
|> to(email_address)
|> from("aemail@gmail.com")
|> subject("Welcome!")
|> put_text_layout({MailerTutorialWeb.LayoutView, "email.text"})
|> render("welcome.text")
end
def welcome_html_email(email_address) do
email_address
|> welcome_text_email()
|> put_html_layout({MailerTutorialWeb.LayoutView, "email.html"})
|> render("welcome.html", email_address: email_address)
end
end
EmailView:
// lib/mailer_tutorial_web/views/email_view.ex
defmodule MailerTutorialWeb.EmailView do
use MailerTutorialWeb, :view
end
Email layouts and template directory structure:
Controller code to send emails:
MailerTutorial.Email.welcome_html_email("anotheremail@gmail.com") |> MailerTutorial.Mailer.deliver_later()
I use four layouts for the email markup email.html.eex
, email.text.eex
, welcome.html.eex
and welcome.text.eex
.
My first question is regarding the modules inside of the .ex
files. What’s common practice for what module should extend from MailerTutorial
and what module should extend from MailerTutorialWeb
? I know it works both ways if you reference it the same way you define it, but from a high conceptual level what modules should be part of the web code and what should be part of the root project?
My second question relates to the templates/layouts
. For some reason if I don’t have the four templates in both the templates/email
directory and the and the templates/layout
directory I get the following error:
Could not render "welcome.text" for MailerTutorialWeb.EmailView, please define a matching clause for
render/2 or define a template at "lib/mailer_tutorial_web/templates/email/*".
No templates were compiled for this module.
or
Could not render "email.text" for MailerTutorialWeb.LayoutView, please define a matching clause for
render/2 or define a template at "lib/mailer_tutorial_web/templates/layout/*".
The following templates were compiled:
Why do I need these templates defined in both? I would prefer to have these in just once place. Please let me know if this is possible and why these errors are being produced.
Thanks,
Scott