# mail

Sending mail through Resend, Cloudflare Email Service or any SMTP server, chosen by a
setting, behind one `mailSend`; a suppression list every send consults,
kept by Resend's events; bodies from Markdown. The whole of it is
[docs/mail.md](../../docs/mail.md).

```text
use "pkg:mail/mail.mote"
use "pkg:mail/webhooks.mote"      // optional: POST /mail/resend/webhook

let body = mailMarkdown("# Thank you\n\nYour order is on its way.")?
mailSend({to: ["ada@shop.example"], subject: "Your order", html: body.html, text: body.text, key: `receipt-${order}`})?
```

Settings: `MAIL_KEY` (a secret), `MAIL_FROM`; `MAIL_PROVIDER=cloudflare`
and `MAIL_ACCOUNT` for Cloudflare; `MAIL_PROVIDER=smtp` and `MAIL_SMTP`
(`smtps://user@host:465`, `smtp://user@host:587`) for SMTP; `MAIL_WEBHOOK_SECRET` (a secret) with
webhooks.mote; `MAIL_API` for a stand-in.

| File | |
|---|---|
| `mail.mote` | `mailSend`, `mailBatch`, `mailCancel`, the two providers' bodies, `MailSuppressed` and its fns. |
| `smtp.mote` | SMTP (STARTTLS or implicit TLS, AUTH PLAIN or LOGIN) on `tcp.open`. |
| `mime.mote` | The MIME message of a `MailMessage` (`mailMime`): what SMTP sends, and SES's and Gmail's raw messages. |
| `markdown.mote` | `mailMarkdown`: HTML with styles in place, and text. |
| `webhooks.mote` | Resend's events, checked as Svix signs them, into the suppression list; `mailHeard`. |
| `parse.mote` | Reading a message: `mailParse(reader)` (its header, every part's place), `mailPiece`/`mailSave`/`mailBody` (a body decoded a piece at a time). Page, `off fn`, back end. [docs/mail-library.md](../../docs/mail-library.md) |
| `mbox.mote` | An mbox, a message at a time: `mailMboxNext(reader)`. |
| `address.mote` | Addresses, Punycode, the Public Suffix List (`psl.mote`, made by `tools/psl-table.mjs`), `mailOrgDomain`. |
| `server.mote` | An SMTP server on `server listen`: `smtpServe(c)`, with the program's `smtpRecipient` and `smtpMessage`; the body kept as it arrives in a plain file, a sealed one or memory (`smtpSpool`), read by `smtpOpen(m)`. |
| `auth.mote` | DKIM (verify, sign), SPF, DMARC, ARC (verify, seal), `Authentication-Results`. |
| `deliver.mote` | Delivery to MX hosts, STARTTLS, MTA-STS, a queue tried again (`mailQueue`), bounces (`mailDsnRead`, `mailDsnMake`). |
| `tests_parse.mote`, `tests_address.mote`, `tests_auth.mote`, `tests_deliver.mote` | The library's tests (tests/mail_lib/unit.mote, `tests/all.sh real_mail_lib`). |
| `tests.mote` | Its tests: `use "pkg:mail/tests.mote"`, then `mote test`. |

**Not yet run against Resend or Cloudflare**: only against stand-ins of
their APIs (`tests/all.sh real_svc_mail`), and SMTP only against a stand-in server (`real_svc_smtp`). The steps to check with real keys
are in the docs.
