ScriptyScott

ScriptyScott

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

Showing Posts 1 to 10

karlosmid

karlosmid

Hi! You need to create the following module as a file (any name is :ok) in your project:

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

ScriptyScott OP

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 #2
pdgonzalez872

pdgonzalez872

Hi @ScriptyScott ! Thanks for sharing this! I will play with Swoosh later this week, so this was a great read. Do you mind sending me a link to your final tutorial when you are done with it? Maybe we can update the docs with something you found out/experienced?

Thanks!

ScriptyScott

ScriptyScott OP

Will do! I’ll post the link, we can go over the docs I used and where they could be a bit more comprehensive.

ScriptyScott

ScriptyScott OP

@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

byronsalty

byronsalty

Me too!

gathubenson

gathubenson

Hello

I’m facing this error ( (elixir 1.15.4) lib/keyword.ex:979: Keyword.merge([otp_app: :email_notification], :email_notification))

kindly help

user20230119

user20230119

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

cobra

cobra

thanks I ran into the same issue and this is a life saver

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
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
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews