# Phoenix

> Send your first email from a Phoenix app by calling the MailBlastr API with an Elixir HTTP client.

The `mailblastr` SDK is for Node.js — Elixir users send email by POSTing JSON to `https://api.mailblastr.com/emails`. The example below uses [`Req`](https://hex.pm/packages/req), a batteries-included HTTP client, called from a Phoenix controller. `HTTPoison` works the same way if you already have it.

## Prerequisites

Before you start, you will need:

- A MailBlastr [API key](https://www.mailblastr.com/docs/api-keys/overview).
- A [verified domain](https://www.mailblastr.com/docs/domains/managing) to send from.
- A Phoenix application.

## 1. Install an HTTP client

Add `req` to your list of dependencies in `mix.exs`, then run `mix deps.get`.

**mix.exs**

```elixir
def deps do
  [
    {:req, "~> 0.5"}
  ]
end
```

## 2. Set your API key

Read the API key from the environment rather than hardcoding it. In `runtime.exs` you can put it into your app config, then read it back where you send.

**config/runtime.exs**

```elixir
config :my_app, :mailblastr_api_key, System.fetch_env!("MAILBLASTR_API_KEY")
```

## 3. Send email from a controller

POST the email body to the MailBlastr API from a controller action. The easiest way to send is with the `html` field.

**lib/my_app_web/controllers/email_controller.ex**

```elixir
defmodule MyAppWeb.EmailController do
  use MyAppWeb, :controller

  def send(conn, _params) do
    api_key = Application.fetch_env!(:my_app, :mailblastr_api_key)

    response =
      Req.post!("https://api.mailblastr.com/emails",
        auth: {:bearer, api_key},
        json: %{
          from: "Acme <onboarding@yourdomain.com>",
          to: ["delivered@example.com"],
          subject: "hello world",
          html: "<strong>it works!</strong>"
        }
      )

    json(conn, %{id: response.body["id"]})
  end
end
```

> **Note:** A successful call returns the created email's `id`. See [Send an email](https://www.mailblastr.com/docs/api/emails-send) for every supported field — `cc`, `bcc`, `reply_to`, `attachments`, `tags`, and `scheduled_at`.
