ScriptyScott

ScriptyScott

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:

Bamboo.Phoenix

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

Marked As Solved Switch mode

kokolegorille

kokolegorille

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
    ...
end

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.

Last Post!

ScriptyScott

ScriptyScott

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.

Thanks,
@kokolegorille

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement