# Elander e-mail: configuration, DNS and operations

Status: **implemented and tested against a local fake SMTP server and a local fake Resend API. Not verified with a real Resend account.** "Accepted" in the delivery log means the provider took the message, not that it reached an inbox.

## 1. Choose a provider (`MAIL_PROVIDER`)

| Variable | Meaning |
|---|---|
| `MAIL_PROVIDER` | `smtp` (default) or `resend` |
| `MAIL_FROM` | Sender, e.g. `Elander <bookings@mail.your-domain.com>`. Must be on a domain verified with the provider. Required in production; `*.local`, `*.test`, `localhost` are refused in production. (`SMTP_FROM` is still read for compatibility.) |
| `MAIL_REPLY_TO` | Optional monitored address for replies |

**Resend** (no extra package; the app calls `https://api.resend.com/emails` with Node's `fetch`)

| Variable | Meaning |
|---|---|
| `RESEND_API_KEY` | `re_...` key with *sending access*. Keep it in the host's secret store, never in git. |
| `RESEND_API_BASE` | Tests/proxies only. Refused in production. |

**SMTP** (any provider that offers SMTP, including Resend's `smtp.resend.com`)

`SMTP_HOST`, `SMTP_PORT`, `SMTP_SECURE` (`true` for port 465), `SMTP_USER`, `SMTP_PASS`. A local host is refused in production.

The configuration is validated when the server starts. A bad value stops the process with `[config] Refusing to start: ...` (the secret is never printed).

Behaviour worth knowing:
- Every message goes through the outbox (`EmailDelivery`): retried with backoff up to 5 times, then DEAD.
- A permanent provider refusal (HTTP 4xx other than 429: unverified domain, invalid recipient, bad key) goes DEAD at once instead of retrying five times.
- Resend requests carry `Idempotency-Key: elander-<outbox id>`, so a retry cannot create a duplicate at the provider.
- Messages that contain a one-time link (password reset, team invitation) have their body removed from the outbox as soon as they are final.

## 2. DNS for the sending domain (do this before the first real send)

Use a sub-domain for transactional mail (e.g. `mail.your-domain.com`) so it does not share reputation with your normal company mail. In the Resend dashboard add the domain; it shows the exact records. Typical set:

| Record | Purpose | Notes |
|---|---|---|
| **SPF** (TXT on the sending/return-path sub-domain) | Authorises the provider's servers | Resend gives the value. Only one SPF record per name; merge `include:` entries if you already have one. |
| **DKIM** (TXT or CNAME, provider-given selector) | Cryptographic signature | Must show *Verified* in the dashboard. |
| **DMARC** (TXT `_dmarc.your-domain.com`) | Policy for failures + reports | Start with `v=DMARC1; p=none; rua=mailto:dmarc@your-domain.com`, read the reports for 2-4 weeks, then move to `p=quarantine`, later `p=reject`. |
| MX on the return-path sub-domain | Bounce handling (if the provider asks) | Provider-given. |

Checklist before launch: domain *Verified* at the provider; `MAIL_FROM` uses it; send a test message to Gmail, Outlook and a Hindi-language mailbox if you have one, and look at "show original" for `spf=pass dkim=pass dmarc=pass`; reply-to is monitored; a person owns the DMARC report mailbox.

Not built (needs a provider account): bounce and complaint webhooks. Until they exist a hard bounce is visible only at the provider.

## 3. Languages

Automated mail is rendered in English or Hindi. Invitee mail (confirmation, reschedule, cancellation, reminder) uses the **organization's default language** (Settings, Organization). Password-reset mail uses the user's own language when it is Hindi, otherwise the organization's; team invitations use the organization's language. Bookings do not store an invitee language yet, so a single organization cannot serve one invitee in English and the next in Hindi; that needs a field on the booking and a language choice on the public page (Phase 5).

## 4. Operating it

- **Settings, Email delivery** (needs `audit.view`; retry needs `settings.edit`) lists messages, their status, tries and last error, with a Retry button for FAILED/DEAD rows. API: `GET /api/email/deliveries?status=&limit=`, `POST /api/email/deliveries/:id/retry`.
- Alert on DEAD rows once monitoring exists (not built).
- Password reset: `POST /api/auth/forgot-password` (always the same answer; 5 requests per IP per minute; 3 mails per address per hour) and `POST /api/auth/reset-password`. Links expire after 60 minutes (reset) or 72 hours (invitation), work once, and sign out all sessions.
- `FRONTEND_URL` (first origin) is used to build the links in the mail, so it must be the public address of the app.
