ScriptyScott

ScriptyScott

Issues Sending Emails with Swoosh

Hey Folks,

I’m trying to send emails with Phoenix 1.6.4 and Swoosh. I was using Bamboo in a previous app and created a test web app to play around with Swoosh. I used the hexdocs page, the Swoosh Git page and various forum posts from here as reference for setting up the mailers and emails.

I created the app as follows mix phx.new swoosh_mailer_tutorial --live.

I used the following configuration in config.exs to send mail via AmazonSES:

config :swoosh_mailer_tutorial, SwooshMailerTutorial.Mailer,
  adapter: Swoosh.Adapters.AmazonSES,
  api_key: "my_api_key"

I used the default mailer provided:

defmodule SwooshMailerTutorial.Mailer do
  use Swoosh.Mailer, otp_app: :swoosh_mailer_tutorial
end

I copied the email creation code almost directly from the Swoosh Git readme:

defmodule SwooshMailerTutorial.SwooshEmail do
  import Swoosh.Email

  def welcome do
    new()
    |> to({"Test User", "test@user.com"})
    |> from({"Dr B Banner", "hulk.smash@example.com"})
    |> subject("Hello, Avengers!")
    |> html_body("<h1>Hello Scott</h1>")
    |> text_body("Hello Scott\n")
  end
end

I then call the mailer and email creator within the PageController’s index/2 method before rendering the standard Phoenix index page:

defmodule SwooshMailerTutorialWeb.PageController do
  use SwooshMailerTutorialWeb, :controller
  require Logger

  def index(conn, _params) do
    SwooshMailerTutorial.SwooshEmail.welcome() |> SwooshMailerTutorial.Mailer.deliver()

    render(conn, "index.html")
  end
end

For some reason this setup produces the following error:

I’m not surer why this is happening. Why can’t my web app find SwooshMailerTutorial.Mailer.deliver/1? I had a similar issue when sending emails using Bamboo and changing SwooshMailerTutorialSwooshMailerTutorialWeb in the mailer and email classes seemed to fix it. This makes no sense to me as the web app should have visibility of both SwooshMailerTutorial and SwooshMailerTutorialWeb. Please let me know what I’m missing.

Thanks,
Scott

Marked As Solved

ScriptyScott

ScriptyScott

Hey Folks,

So this took some time but I eventually figured it out. There were multiple issues and problems sending emails with Swoosh and Amazon SES. Also, since Swoosh has only just recently come packaged with Phoenix (Phoenix 1.6.x), the tutorials and usage guides for using it with Amazon SES are spotty at best. I basically had to use multiple sources that all focused on different details of this implementation to stitch together a working solution. Because of this, I’ll be authoring an in-depth tutorial on how to use Swoosh with the Amazon SES adaptor and the SMTP adapter using the Amazon SMTP endpoint and a SMTP IAM user. I was able to produce working solutions with both adapters. However, both implementations have their own pros and cons. I’ll be outlining this and the entire process in a tutorial that will be posted to my company’s blog. I’ll make sure to circle back and update this reply with the link when it’s completed in the coming months.

The solution for the original issue was rather simple:

The problem was that if you use any adaptors other than the Swoosh.Adapters.SMTP you need to include the gen_smtp dependency as the other adapters rely on gen_smtp as a SMTP client. To get passed the SwooshMailerTutorial.Mailer.deliver/1 is undefined error I just simply added gen_smtp as a dependency and updated.

Add the gen_smtp dependency to mix.exs deps:

  defp deps do
    [
      {:gen_smtp, "~> 1.1.1"}
    ]
  end

Then run mix deps.get --all and restart your server. Adding this as a dependency was mentioned in multiple docs and guides but I must have missed it. After fixing this issue I was then faced with the following:

[error] GenServer #PID<0.749.0> terminating
** (UndefinedFunctionError) function false.post/4 is undefined (module false is not available)

The solution for this error was less documented but basically consisted of adding another dependency. It turns out that Swoosh requires an API client if you use an adapter other than the SMTP. This is initially set to false in the config.exs:

# Swoosh API client is needed for adapters other than SMTP.
config :swoosh, :api_client, false

This should be changed to:

config :swoosh, :api_client, Swoosh.ApiClient.Hackney

And the Hackney dependency needs to be added to mix.exs deps:

  defp deps do
    [
      {:hackney, "~> 1.18.0"}
    ]
  end

Once again run mix deps.get --all before restarting your server.

This was my final issue, and after resolving a couple of problems with my AmazonSES configurations, I was able to successfully send emails. I hope this reply helps and I’ll update it with my final tutorial when it’s up.

Regards,
Scott

26
Post #3

Also Liked

user20230119

user20230119

I had to comment config :swoosh, :api_client, false in dev.exs.

ScriptyScott

ScriptyScott

@pdgonzalez872 Created a discussion thread to talk about the pros and cons of using the Swoosh.Adapters.SMTP and Swoosh.Adapters.AmazonSES adapters.

krp

krp

Thank you! You pulled me out of a rabbit hole

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
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
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

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42158 114
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

We're in Beta

About us Mission Statement