Issues Sending Emails with Swoosh

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 Likes