# Setting up real email (SMTP) for Elander

Status when written (2026-10-29): `backend/.env` has `SMTP_HOST=localhost`, `SMTP_PORT=1025`, no credentials, and nothing is listening on 1025
(`docker-compose.yml` defines a Mailpit catcher on 1025/8025, but Docker/Mailpit are not installed on this machine). Until SMTP is configured,
booking, reschedule, cancel and reminder emails are recorded as `FAILED` in the `EmailDelivery` collection and retried (5 attempts, backoff
2/4/8/16 min) instead of being reported as sent.

## Variables (all read by `backend/src/app.ts` from `backend/.env` or the process environment)
| Variable | Meaning |
|---|---|
| `SMTP_HOST` | provider's SMTP host name |
| `SMTP_PORT` | `587` (STARTTLS, recommended) or `465` (implicit TLS) |
| `SMTP_SECURE` | `false` for 587, `true` for 465 |
| `SMTP_USER` / `SMTP_PASS` | provider SMTP credentials (an app password or API key, never your personal login password) |
| `SMTP_FROM` | e.g. `Elander <no-reply@your-domain>`; must be a sender/domain the provider has verified |

## Secure procedure
1. Create an SMTP credential at the provider (Google Workspace app password, Microsoft 365 SMTP AUTH, SendGrid, Amazon SES, Postmark, Mailgun...). Verify the sending domain and add its SPF and DKIM (and ideally DMARC) DNS records, otherwise mail lands in spam.
2. Edit `backend/.env` **yourself** in an editor. Do not paste the password into chat, tickets, git or logs. `.env` is git-ignored; keep it that way. Restrict the file to your Windows user if the machine is shared.
3. Set the five variables above. Leave `SMTP_USER/SMTP_PASS` empty only for a local catcher.
4. Check without sending anything: `cd backend; node scripts/smtp-check.mjs` (prints host/port/secure and whether user/pass are set, never the secrets; verifies connect + login).
5. Send one message to a mailbox you control: `node scripts/smtp-check.mjs --send-to you@your-domain.com`. Nothing is sent without `--send-to`.
6. Restart the backend (environment is read at start). Then book a test slot for your own address and confirm the delivery row shows `SENT`.
7. Rotate the credential if it is ever exposed; use a provider API key with send-only scope where possible.

## Reading delivery status
Each message is a row in `EmailDelivery` (`PENDING`, `SENDING`, `SENT`, `FAILED` = will retry, `DEAD` = gave up after 5 attempts, `CANCELLED` = booking changed first) with `attempts` and `lastError`. The booking API response includes `emailStatus`, and the UI toast says "could not be sent yet" unless it is `SENT`.
