miguelszerman

miguelszerman

Swoosh Sendgrid send email error 400 - How should I approach debugging it?

I am trying to send emails using Sendgrid with Swoosh. I tried the HTTP request and sent it with Postman, it works fine. When I try to do it with Swoosh, I get the error code 400 with Nginx - Bad Request.

How should I approach debugging this? I considered going through the library itself to understand what might be different between my request in Postman and Swoosh (since it’s unclear to me how Swoosh builds the HTTP request behind scenes).

defmodule PhoenixApp.PrepareEmail do
  use Phoenix.Swoosh, view: PhoenixAppWeb.EmailView, layout: {PhoenixAppWeb.EmailView, :email_root}

  def create_email(conn, email) do
    new()
    |> to({"someone", email})
    |> from({"...name...", "...email..."})
    |> reply_to({"...name...", "...email..."})
    |> subject("...subject...")
    |> text_body("hey there")
  end
end

defmodule PhoenixApp.Mailer do
  use Swoosh.Mailer, otp_app: :phoenix_app
end

defmodule PhoenixAppWeb.PageController do
  use PhoenixAppWeb, :controller

  def index(conn, _params) do

    # Create Email
    email = PhoenixApp.PrepareEmail.create_email(conn, email)

    # Send Email
    PhoenixApp.Mailer.deliver(email)
  
    render(conn, "index.html")
  end
end

First Post!

w0rd-driven

w0rd-driven

This comes from little experience with Swoosh but more of lending you brain power.

Does the email variable match what you’re sending in Postman? I can’t tell if that’s generic text for illustration purposes. I ask because sometimes you have to send what looks like an email address.

You could try tacking on an IO.inspect() call to your deliver method to see if anything returns there but I suspect the error you’re seeing on screen comes from an error being raised. You could try using case… with {:ok, result} and {:error, error} tuples or try to trap the error if it’s from a raise. I thought that’s supposed to only happen with the functions with ! on the end though. You should be able to inspect at the boundary you’re at to get some insight over just a 400 response.

If this were me and that all didn’t work then I’d start unpacking what deliver did with more inspect calls or essentially what you said. I don’t always try to peel back a library’s stack traces unless I think I’ve exhausted what I can at the outer most layers. Bad requests don’t usually come from erroneous data, it usually comes from sending a GET where a POST is necessary. Not all APIs are like that though. Sometimes a missing header or something random triggers it too.

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40042 209
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement