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
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 ![]()
Best of luck though and I am curious what the solution is
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.
Popular in Questions
Other popular topics
Latest Phoenix Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









