Functional Weave
Code in TypeScript

email.send@1.0.0

README.md

1,434 bytes · view raw

# email.send

Send one transactional email (a welcome, a password reset, a receipt) through
whichever provider the environment chooses, with the same call everywhere:

```ts
import { sendEmail } from "#fune/email.send@^1";

await sendEmail({ to: ["ada@example.com"], subject: "Welcome", text: "Hello Ada" });
```

`EMAIL_PROVIDER` picks the provider:

- **resend** posts to Resend's HTTP API with `RESEND_API_KEY`.
- **smtp** talks to the server in `SMTP_URL`: `smtps://user:password@host:465`
  for implicit TLS, or `smtp://user:password@host:587`, which it upgrades with
  STARTTLS and refuses to use without it. The message is MIME, each body
  base64 in lines of 76, with a `Message-ID` on the sender's domain.
- **log** sends nothing: it writes one line per message to the log, which is
  what development, preview deployments and an application's own tests want.

`EMAIL_FROM` is the From address when a message gives none.

The message is checked before anything is sent: at least one recipient, every
address an address, a one-line subject, a text or an HTML body, and a From.
What the provider refuses is thrown with its status and its own reason.

The contract tests replay Resend's documented responses (success, a
validation error, a bad key) and complete SMTP dialogues (implicit TLS,
STARTTLS, a refused login), offline. They are not a substitute for sending
one real email from a staging environment before going live.