Swoosh Sendgrid adapter returns OK but no email is set

Hello All

I am a total newbie with Elixir/Phoenix and am having problems sending emails using Swoosh and Sendgrid.

The mix.exs has

{:swoosh, "~> 1.0"},
{:gen_smtp, "~> 0.13"},
{:phoenix, "~> 1.5.5"},
{:hackney, "~> 1.17"},

And looks like everything follows the docs exactly:


"Swoosh.Adapters.Sendgrid" ->
    config :chat_api, ChatApi.Mailers,
      adapter: Swoosh.Adapters.Sendgrid,
      api_key: System.get_env("SENDGRID_API_KEY")


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

App Code:

defmodule ChatApi.Mailers do
  use Swoosh.Mailer, otp_app: :chat_api

 @spec send_password_reset_email(User.t()) :: deliver_result()
  def send_password_reset_email(user) do
    user |> Email.password_reset() |> deliver()

@spec deliver(Email.t()) :: deliver_result()
  def deliver(email) do
    try do
      if has_valid_to_addresses?(email) do
        ChatApi.Mailers.deliver(email) |> IO.inspect(label: "sent")
        {:warning, "Skipped sending to potentially invalid email: #{inspect(email.to)}"}
      e ->
          "Email config environment variable may not have been setup properly: #{e.message}"

        {:error, e.message}

Upon executing the send email function, i simply get a:

sent: {:ok, %{id: "98ce675247d8e7e2629e72f2ec4b85cc"}}

But no email arrives and Sendgrid dashboard shows no request was ever made to their SMTP servers.

I really don’t know how else to debug or troubleshoot this and would appreciate any help or guidance.

Thank you.

I’ve never used that adapter for Swoosh before, but looking at the code, it doesn’t use SMTP, instead it calls their their API with a JSON payload.

The ID you see in the result comes from the x-message-id header of the response from Sendgrid. Maybe check elsewhere in Sendgrid’s dashboard for related logs

I had a similar issue with Finch where I needed a separate instance in application.ex as I was already using it as my normal HTTP client. I am unfamiliar w/ Hackney but just figured I’d mention it.

Thanks, yes you are right, its a simple API call to Sendgrid. The Sendgrid dashboard includes API calls because I just tried a sample plain vanilla NodeJS code which calls the API to send emails and it was successful without any issues.

this also rules out any firewall issues as the sample NodeJS was running on the same VM network as my Elixir app.

Is there any way to see more debugging information? Doesn’t have to be just Swoosh specific, just any way to see more information. It is quite hard to know where to start looking when you don’t know what is wrong and you get a success response.

I saw your thread, but couldn’t really apply it to my app since its not Finch. In addition, I actually don’t understand it lol

I’ve set up a webhook on Sendgrid side to watch for any activity.

using Swoosh, there is no activity recorded while a dummy NodeJS script (which was successful in sending an email), did indeed get captured by the webhook.

conclusion for now is, Swoosh is not actually contacting Sendgrid API servers at all. What is so weird is that the x-message-id that is returned, is actually bogus. I tried looking up the x-message-id with Sendgrid’s endpoints and could not get anything.

why would Swoosh do this?

Okay yeah I just remember having similar errors, where I was getting a success, and an ID but SendGrid was showing nothing at all, but then it would work with the same account using cURL or other libraries for other languages. I just thought I would mention it as something to look out for. In the application.ex file I already had a
{Finch, name: MyApp.Finch}

that was my normal HTTP client, and for whatever reason I needed to add a second one for Swoosh.

      {Finch, name: MyApp.Finch},
      {Finch, name: Swoosh.Finch},

which I am still not convinced is the correct way to go about it, but it did get me over the hump and got the email delivered. So just maybe something to look out for but again not familiar with Hackney, and it might be completely different circumstances anyway :slight_smile:

Best of luck though and I am curious what the solution is

1 Like

thanks. i re-wrote the various files to use Finch and also made sure I have that line in application.ex

still same problem.

its so baffling to me that Swoosh mailer returns OK and an bogus x-message-id; why do this?

Ok, closing the loop on this. Root cause: total user error.

Full transparency i am forking on a repo of an abandoned open source product. the product was abandoned 3 years ago but it has almost every feature i needed.

turns out the problem is that the app never even used the Sendgrid adapter, it was defaulting to a Swoosh.Adapters.Local which i guess sent out dummy emails and returns a successful response. I was erroneously setting the environment variable in Dockerfile when I should have set it in docker-compose. As soon as I set it, the ‘right’ kind of errors came out letting me know what else I was missing. Because I am using Swoosh 1.0, it actually cannot use Finch and must use Hackney. I reverted to Hackney, and voila, emails flowed.

This prob wont help anyone in the future because it was such a dumb mistake, but just wanted to end the suspense.

1 Like