Guide · .NET & Elixir
How to send email with Elixir and Phoenix
Send email from Elixir and Phoenix with the Req HTTP client and the Stack Emails API.
Last reviewed November 2026
1. Set up
You need a Stack Emails API key (create one free) and a verified sending domain.
# mix.exs
{:req, "~> 0.5"}
2. Send your first email
defmodule Acme.Mailer do
def send_welcome(email) do
Req.post!("https://stackemails.com/v1/emails",
auth: {:bearer, System.fetch_env!("STACKEMAILS_API_KEY")},
json: %{
from: "Acme <hello@mail.acme.com>",
to: email,
subject: "Welcome to Acme",
html: "<p>Thanks for signing up!</p>"
}
)
end
end
A successful request returns 202 with the message id, for example { "id": "msg_5b1f0c6c2a8e4e0f", "status": "queued" }. Errors return a JSON body with error.code and error.message (see errors).
Using Swoosh? Its SMTP adapter (with gen_smtp) works with relay: "smtp.stackemails.com", port: 465, ssl: true, username: "stackemails" and your API key as the password.
Production checklist
- Keep the API key in an environment variable or secret manager, never in client-side code.
- Add an
Idempotency-Keyheader to retries so a timeout never sends twice (idempotency). - Subscribe to webhooks for bounces and complaints.
- Check your domain's SPF, DKIM and DMARC records.
- Start from a tested email template for password resets, receipts and invites.