ScriptyScott

ScriptyScott

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

Showing Posts 1 to 2

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.

ScriptyScott

ScriptyScott OP

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews