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

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.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New

Other popular topics Top

New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement