Supabase

Supabase Auth custom SMTP with Hermaily

Supabase's built-in email service is meant for testing and only sends to your team. For real users, Auth needs a custom SMTP server: with Hermaily, confirmations, magic links, invitations and password resets go out from your domain, with DKIM and with their deliveries and bounces visible.

  • SMTP smtp.hermaily.com:587
  • Supabase Cloud, CLI and self-hosted
  • Edge Functions: API POST /v1/send

Before you start

Three things from Hermaily before the first message:

  1. An account. Create one (there is a free plan). New accounts are reviewed before they can send; you can set everything up in the meantime.
  2. A verified domain. In Domains, the assistant gives you the DKIM, SPF and DMARC records and checks they are published. You can only send from addresses on that domain or its subdomains.
  3. An API key for that domain, created in API keys. It starts with hm_ and is shown only once: keep it in your secrets manager or in the environment, never in the repository. The same key is the SMTP password; the username is apikey.

Configuration

In the Supabase dashboard, open your project, go to Authentication → Emails → SMTP Settings, turn on custom SMTP and fill in:

Supabase · SMTP Settings
Sender email     auth@acme.example        (on your verified domain)
Sender name      Acme
Host             smtp.hermaily.com
Port number      587
Username         apikey
Password         hm_your_api_key

Use 465 if you prefer implicit TLS. Supabase applies its own limit on how many Auth emails it sends per hour; once custom SMTP is on you can change it under Authentication → Rate Limits, within your Hermaily plan's per-minute limit. Sign-in emails cannot wait: on the Free plan, sends may be delayed at busy times, so for production a paid plan is better.

For local development with the Supabase CLI:

TOML
# supabase/config.toml (local development with the Supabase CLI)
[auth.email.smtp]
enabled = true
host = "smtp.hermaily.com"
port = 587
user = "apikey"
pass = "env(HERMAILY_API_KEY)"
admin_email = "auth@acme.example"
sender_name = "Acme"

And on self-hosted Supabase, in the Docker .env (Auth reads them as GOTRUE_SMTP_*):

.env
# Self-hosted Supabase (docker/.env)
SMTP_ADMIN_EMAIL=auth@acme.example
SMTP_HOST=smtp.hermaily.com
SMTP_PORT=587
SMTP_USER=apikey
SMTP_PASS=hm_your_api_key
SMTP_SENDER_NAME=Acme

Send a message

Supabase sends Auth emails on its own: try it by signing up a test user, or with Send magic link or Send password recovery on a user in Authentication → Users. The message appears in Hermaily under Messages within seconds. The templates are still edited in Supabase (Authentication → Emails → Templates); Hermaily sends them as they are.

Your own emails from an Edge Function

For receipts, notifications and the like, call the HTTP API from an Edge Function, with the key stored as a secret:

TypeScript · Deno
// supabase/functions/send-receipt/index.ts
// supabase secrets set HERMAILY_API_KEY=hm_... MAIL_FROM="Acme <hello@acme.example>"
Deno.serve(async (req) => {
  const { to, orderNumber } = await req.json();
  const res = await fetch("https://api.hermaily.com/v1/send", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${Deno.env.get("HERMAILY_API_KEY")}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      from: Deno.env.get("MAIL_FROM"),
      to,
      subject: `Receipt for order ${orderNumber}`,
      text: `Thanks for your order ${orderNumber}.`,
      tags: ["receipt"],
    }),
  });
  return new Response(await res.text(), { status: res.status, headers: { "Content-Type": "application/json" } });
});

Protect the function (keep JWT verification on, and check the user's permissions) so it cannot be used to send to arbitrary addresses.

Events and webhooks

Every message gets an id: over the API it comes in the 202 response and over SMTP it comes in the server's reply to the message (250 2.0.0 Ok: queued as <id>). Accepted means queued, not delivered. To know what happened next:

  • Dashboard. Messages lists each message with its events: delivered, deferred, bounced, complaint, opened, clicked.
  • API. GET https://api.hermaily.com/v1/messages/{id} returns the current status and every event, oldest first (see looking up a message).
  • Webhooks. In Webhooks, add your URL (https and public) and the events you want. Hermaily makes one POST per event, signed with X-Hermaily-Signature: t=<unix>,v1=<hex>, where v1 is HMAC-SHA256(secret, t + "." + body) over the raw body. Answer any 2xx within 10 seconds; otherwise it is retried for about a day and a half. Delivery is at least once, so discard duplicates by the event id. The Test button sends a sample event to check your receiver.

To react to bounces of your users' addresses (to mark an email as invalid, for example), receive the webhooks in another Edge Function:

TypeScript · Deno
// supabase/functions/hermaily-webhook/index.ts
// Deploy without JWT check (Hermaily signs with its own secret):
//   supabase functions deploy hermaily-webhook --no-verify-jwt
const enc = new TextEncoder();
const key = await crypto.subtle.importKey(
  "raw", enc.encode(Deno.env.get("HERMAILY_WEBHOOK_SECRET")!), { name: "HMAC", hash: "SHA-256" }, false, ["verify"],
);

Deno.serve(async (req) => {
  const raw = await req.text();
  const parts = Object.fromEntries((req.headers.get("x-hermaily-signature") ?? "").split(",").map((p) => p.split("=")));
  const t = Number(parts.t);
  const sig = new Uint8Array(((parts.v1 ?? "").match(/../g) ?? []).map((h: string) => parseInt(h, 16)));
  const fresh = t && Math.abs(Date.now() / 1000 - t) <= 300;
  if (!fresh || !(await crypto.subtle.verify("HMAC", key, sig, enc.encode(`${t}.${raw}`)))) {
    return new Response("invalid signature", { status: 401 });
  }
  const event = JSON.parse(raw);
  // event.type: "bounce", "complaint"...; event.recipient
  return new Response(null, { status: 204 });
});

Event types: send, delivery, delivery_delay, bounce, complaint, reject, rendering_failure, open, click and unsubscribe (opens and clicks only with tracking on). Permanent bounces, complaints and unsubscribes are suppressed automatically, so you do not need to keep your own list for that. Payload, fields and signature details are in the webhook documentation.

Troubleshooting

The errors you are most likely to see, with what they mean. The full list is in errors and limits.

Error sending confirmation email (or magic link) in the app

Supabase hides the SMTP reply from the client. Open Logs → Auth in the dashboard: the entry carries Hermaily's answer, usually one of the ones below. Also check that Sender email is on your verified domain.

535 5.7.8: authentication failed

The password must be the whole API key (hm_...), copied without spaces and not revoked. The username can be anything; use apikey. After many failed attempts from the same IP, logins are refused for a few minutes.

550 5.7.1: the message is refused

The account cannot send right now (pending approval, paused, or new terms to accept), the key is restricted to the other stream, or the From address is not on the key's domain. Over the API this is 403 forbidden.

550 5.6.0: the message is rejected

Usually the From domain is not verified yet, there are more recipients than your plan allows (5 on Free, 50 on paid plans) or the message is too large. Over the API this is 422 validation, with the reason in the message.

451 4.7.1: try again later

The plan's per-minute limit or a new account's daily warm-up limit. It is temporary: retry later (over the API, 429 rate_limited). 552 5.3.4 means the monthly quota of the Free plan is used up (402 quota_exceeded).

Send your first message today

Create the account, verify your domain and paste the key. The free plan is enough to try it.

Create a free account