TypeScript client for sending emails via the @schemavaults/mail-server API. Provides request body validation (via Zod), environment-aware server URL resolution, and helpers for sending to individual recipients, mailing lists, and listing available email templates.
bun add @schemavaults/send-email
# or
npm install @schemavaults/send-email| Variable | Required | Description |
|---|---|---|
SCHEMAVAULTS_MAIL_API_KEY |
Yes | Bearer token for the mail-server API. Starts with svlts_mail_pk_. |
SCHEMAVAULTS_MAILING_LIST_ID |
For mailing list sends | UUID of the target mailing list. |
SCHEMAVAULTS_APP_ENVIRONMENT |
No | "production" (default), "development", or "staging". Controls which mail-server instance is targeted. |
import { sendEmail } from "@schemavaults/send-email";
// Using a registered template
await sendEmail({
body: {
to: "user@example.com",
subject: "Welcome!",
message: {
template_id: "welcome-email",
template_props: { name: "Alice" },
},
},
});
// Using raw text/html
await sendEmail({
body: {
to: "user@example.com",
subject: "Hello",
message: {
text: "Hello from SchemaVaults.",
html: "<p>Hello from SchemaVaults.</p>",
},
},
});import { sendEmailToMailingList } from "@schemavaults/send-email";
await sendEmailToMailingList({
body: {
subject: "Weekly update",
message: {
text: "Here's what happened this week.",
html: "<p>Here's what happened this week.</p>",
},
},
});The mailing list UUID is read from SCHEMAVAULTS_MAILING_LIST_ID by default. You can override it per-call:
await sendEmailToMailingList({
mailingListId: "00000000-0000-0000-0000-000000000000",
body: { subject: "...", message: { text: "...", html: "..." } },
});import { listEmailTemplates } from "@schemavaults/send-email";
const templates = await listEmailTemplates();
// => [{ id: "welcome-email", description: "..." }, ...]The package exports the Zod schema used by both this client and the mail-server to validate request bodies:
import { createSendEmailRequestBodySchema } from "@schemavaults/send-email";
// Pass `true` to allow mailing list UUIDs in the `to` field
const schema = createSendEmailRequestBodySchema(true);
const result = schema.safeParse(body);Sends an email to one or more recipients.
interface ISendEmailOpts {
body: SendEmailRequestBody;
bearerToken?: string; // overrides SCHEMAVAULTS_MAIL_API_KEY
mailServerUrl?: string; // overrides resolved server URL
environment?: "production" | "development" | "staging";
}Sends an email to a mailing list audience. The to, cc, and bcc fields are not accepted -- the audience is the mailing list.
interface ISendEmailToMailingListOpts {
body: Omit<SendEmailRequestBody, "to" | "cc" | "bcc">;
mailingListId?: string; // overrides SCHEMAVAULTS_MAILING_LIST_ID
bearerToken?: string;
mailServerUrl?: string;
environment?: "production" | "development" | "staging";
}Returns the list of registered email templates from the mail-server catalog.
interface IListEmailTemplatesOpts {
bearerToken?: string;
mailServerUrl?: string;
environment?: "production" | "development" | "staging";
}
interface EmailTemplate {
id: string;
description: string;
}Reads and returns SCHEMAVAULTS_MAIL_API_KEY from the environment. Throws if not set.
Returns a Zod schema for validating send-email request bodies. Pass true to allow UUIDs (mailing list IDs) in the to field.
Zod schema for validating template IDs: lowercase alphanumeric with hyphens/underscores, 1-64 characters, must start with a letter.
The message field accepts either a template reference or raw content:
type Message =
| { template_id: string; template_props?: unknown }
| { text: string; html: string }; // both required
type SendEmailRequestBody = {
to: string | string[]; // email address(es) or mailing list UUID
subject: string;
message: Message;
from?: string; // defaults to mail-server's configured sender
replyTo?: string;
cc?: string | string[];
bcc?: string | string[];
};bun install
bun run build
bun test
bun run typecheck
bun run lint