Skip to main content

Setup

# mix.exs
defp deps do
  [{:req, "~> 0.5"}]
end

Send an email

{:ok, response} = Req.post("https://app.usesendi.com/api/emails",
  headers: [
    {"authorization", "Bearer snd_your_api_key"},
    {"content-type", "application/json"}
  ],
  json: %{
    from: "you@yourdomain.com",
    to: ["user@example.com"],
    subject: "Hello from Sendi",
    html: "<p>It just works.</p>"
  }
)

IO.inspect(response.body["id"])

With Phoenix (Swoosh)

Create lib/my_app/sendi_adapter.ex:
defmodule MyApp.SendiAdapter do
  @behaviour Swoosh.Adapter

  @impl true
  def deliver(%Swoosh.Email{} = email, _config) do
    {:ok, response} = Req.post("https://app.usesendi.com/api/emails",
      headers: [
        {"authorization", "Bearer #{System.get_env("SENDI_API_KEY")}"},
        {"content-type", "application/json"}
      ],
      json: %{
        from: format_address(email.from),
        to: Enum.map(email.to, &format_address/1),
        subject: email.subject,
        html: email.html_body,
        text: email.text_body
      }
    )

    {:ok, response.body}
  end

  defp format_address({name, email}), do: "#{name} <#{email}>"
  defp format_address(email), do: email
end