Questions Sending Emails via Bamboo

Hey Folks,

Recently I added email functionality to my phoenix app. I used these two tutorials as reference:

  1. Sending Email with SMTP
  2. How to Send Emails From Phoenix in 4 Easy Steps

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

Project email:

// lib/mailer_tutorial/mailer.ex
defmodule MailerTutorial.Email do
  use Bamboo.Phoenix, view: MailerTutorialWeb.EmailView

  def welcome_text_email(email_address) do
    |> to(email_address)
    |> from("")
    |> subject("Welcome!")
    |> put_text_layout({MailerTutorialWeb.LayoutView, "email.text"})
    |> render("welcome.text")

  def welcome_html_email(email_address) do
    |> welcome_text_email()
    |> put_html_layout({MailerTutorialWeb.LayoutView, "email.html"})
    |> render("welcome.html", email_address: email_address)


// lib/mailer_tutorial_web/views/email_view.ex
defmodule MailerTutorialWeb.EmailView do
  use MailerTutorialWeb, :view

Email layouts and template directory structure:

Controller code to send emails:

MailerTutorial.Email.welcome_html_email("") |> 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.


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.


1 Like

I did not read the tutorials You have linked, but I can tell You what is different in my config.

I do not have a separation between text/html in the mailer… what I have is similar to

def welcome_email(email_address) do

I also don’t have template duplication between email/layout. It’s all in templates/email, nothing in layout. And there, I have a welcome.html.eex and welcome.text.eex.

BTW Phoenix 1.6 will ship with swoosh.

1 Like

Woow, swoosh looks far simpler than bamboo and it ships with an SES adaptor! The posted code is from a test project I had setup so that I could familiarize my self with sending emails in phoenix before implementing it into my marketing site. The marketing site is running phoenix v1.5.9 so I think I’ll just wait until 1.6 is out (Chris McCord seems to think that’s fairly soon) and I’ll update the site and send with swoosh.