santafecap

santafecap

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:

runtime.exs

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

config.exs

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

App Code:

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

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

@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")
      else
        {:warning, "Skipped sending to potentially invalid email: #{inspect(email.to)}"}
      end
    rescue
      e ->
        IO.puts(
          "Email config environment variable may not have been setup properly: #{e.message}"
        )

        {:error, e.message}
    end
  end

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.

Most Liked

r4z4

r4z4

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

santafecap

santafecap

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.

Where Next?

Popular in Questions Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement