# Email delivery and deliverability

How to get the Herald email into the inbox instead of the spam folder. The notifier itself is
described in [Channels: Email subscribers](channels.md#email-subscribers); consent and retention rules are in
[`privacy.md`](privacy.md).

## Choose a sending domain

Send from a domain you control, never from a free mailbox (`@gmail.com`, `@outlook.com`): those
domains publish strict DMARC policies, so mail "from" them sent through anyone else's servers is
rejected. A subdomain such as `news.example.com` keeps the briefing's reputation separate from
your personal or company mail.

## SPF, DKIM and DMARC

Gmail and Yahoo require all three from bulk senders (since February 2024), and other providers
weigh them heavily. Your provider (Resend, your SMTP service) shows the exact values to publish
after you add the domain; the shapes are:

| Record | Name | Type | Value (example) | What it proves |
|---|---|---|---|---|
| SPF | `news.example.com` (or the provider's bounce subdomain, e.g. `send.news.example.com`) | TXT | `v=spf1 include:<provider's SPF domain> ~all` | The server is allowed to send for the domain |
| DKIM | `<selector>._domainkey.news.example.com` | TXT or CNAME | the provider's public key | The message was signed by the domain and not changed |
| DMARC | `_dmarc.news.example.com` | TXT | `v=DMARC1; p=none; rua=mailto:dmarc@example.com; adkim=r; aspf=r` | What receivers do when SPF/DKIM fail, and where to send reports |

- **One SPF record per name.** If the domain already has one, add the new `include:` to it;
  two `v=spf1` records make both invalid. SPF allows at most 10 DNS lookups.
- **DMARC alignment.** DMARC passes when SPF or DKIM passes *for the domain in `From`*. With
  Resend and most API providers, DKIM is signed with your domain, so DKIM alignment is what
  counts.
- **Tighten DMARC gradually.** Start with `p=none` and read the aggregate reports (`rua`) for a
  couple of weeks; when only your senders appear, move to `p=quarantine`, then `p=reject`.
- Check the records with `dig TXT _dmarc.news.example.com +short` (and the same for the SPF and
  DKIM names), or with the provider's domain verification page.

## Deliverability checklist

- [ ] `from` uses your own (sub)domain, and SPF, DKIM and DMARC pass for it.
- [ ] Reverse DNS (PTR) matches the sending host, if you run your own SMTP server.
- [ ] `unsubscribe_base_url` is an `https://` endpoint you run that accepts both `GET` (the link
      in the footer) and `POST` with `List-Unsubscribe=One-Click` (the button Gmail and Yahoo
      show, RFC 8058), and calls `herald subscribers unsubscribe <token>`. Without it, Herald
      falls back to a `mailto:` unsubscribe, which large providers accept but rank lower.
- [ ] `privacy_url` points to your published privacy notice.
- [ ] Only double opt-in addresses (Herald enforces this): never import a bought or scraped list.
- [ ] Bounced addresses stay inactive (Herald marks them on permanent refusals); with Resend,
      also check the dashboard for asynchronous bounces and complaints and remove those
      addresses with `herald subscribers unsubscribe`.
- [ ] Spam complaint rate below 0.3% (Gmail's limit; aim for under 0.1%). Watch it in
      [Google Postmaster Tools](https://postmaster.google.com/).
- [ ] Ramp up a new domain: a few dozen recipients in the first days, then grow.
- [ ] Send at a steady time every day; sudden spikes look like spam.
- [ ] Test before going live: send to [mail-tester.com](https://www.mail-tester.com/) and to
      your own Gmail, Outlook and Apple Mail accounts, in light and dark mode.

## What the template does for you

- Table layout with inline styles, a 600 px column and an Outlook (MSO) wrapper; no scripts, no
  web fonts, no tracking pixels, no click tracking.
- Light and dark palettes from the brand (`prefers-color-scheme` and Outlook.com's dark mode);
  clients that invert colours on their own still keep readable contrast.
- A hidden preheader with the first headlines, so the inbox preview is useful.
- A plain-text alternative (links as `label (url)`).
- The HTML stays under Gmail's 102 KB clipping limit, past which Gmail hides the rest of the
  message and its unsubscribe link. A briefing too long for it (about 50 stories) shows as many
  whole sections as fit, then a note and, with `site.enabled` and `site.base_url`, a link to the
  full briefing page. The plain-text part always has the whole briefing.
- The logo is a PNG (Gmail does not show SVG): `logo_url`, or `email-logo.png` /
  `email-logo-dark.png` that the static site writes next to the briefings when `site.enabled`
  and `site.base_url` are set. Without either, the name is set in text.

## Test locally with Mailpit

[Mailpit](https://mailpit.axllent.org/) is a local SMTP server with a web inbox, handy to look at
the email before sending it to anyone. This is a manual check, not part of the test suite or CI;
`scripts/e2e-local.sh email` runs it automatically (see [release-checklist.md](release-checklist.md)).

```bash
docker run --rm -d --name mailpit -p 8025:8025 -p 1025:1025 axllent/mailpit
```

Point the email notifier at it in a throwaway config (keep the real one untouched):

```json
{ "type": "email", "enabled": true, "languages": ["pt-BR"], "transport": "smtp",
  "smtp_host": "127.0.0.1", "smtp_port": 1025, "starttls": false,
  "from": "Herald <herald@example.com>",
  "unsubscribe_base_url": "https://example.com/unsubscribe" }
```

Add yourself with `herald subscribers add you@example.com --lang pt-BR --confirmed`, run
`herald run` with a temporary `data_dir`, then open <http://localhost:8025>. Mailpit shows the
HTML, the plain-text part, the headers (`List-Unsubscribe`, `List-Unsubscribe-Post`) and an HTML
compatibility report per client. Stop it with `docker stop mailpit`.
