Send email from Next.js
Install mailset, send from server code, and take delivery events in a route handler. The one trap is the webhook’s body: read it with request.text(), never request.json().
By starting you agree to the Terms and Privacy Policy.
Install the TypeScript SDK
The same mailset package as any Node.js app, from your package manager. It has no dependencies of its own for the bundler to pull in.
npm install mailset
Send your first email from Next.js
Call it from server code only: a route handler, a server action or a script. new Mailset() reads MAILSET_KEY from the server’s environment, deliverBy: '10m' refuses a code that would arrive too late, and the receipt carries id and status.
import { Mailset } from 'mailset';
// Reads MAILSET_KEY from the environment.
const mailset = new Mailset();
const sent = await mailset.send({
from: 'Acme <moc.elpmaxe@olleh>',
to: 'gro.elpmaxe@ana',
subject: 'Your Acme code',
text: 'Your code is 123456.',
html: '<p>Your code is <b>123456</b>.</p>',
deliverBy: '10m',
});
console.log(sent.id, sent.status);
In the SDK’s tests: type-checked as a route handler.
Verify webhooks in a route handler
The file exports POST. It reads the body with request.text(), because the signature covers the bytes as they came and request.json() would throw them away. webhooks.verify checks them with request.headers and MAILSET_HOOK_SECRET; a MailsetError answers 401 with its code. A hard bounce is logged, and 204 tells Mailset the batch is kept.
// app/api/mailset/route.ts: a Next.js route handler.
import { MailsetError, webhooks } from 'mailset';
export async function POST(request: Request) {
// The signature covers the body as it came, so read
// it as text: request.json() would lose those bytes.
const body = await request.text();
try {
const events = await webhooks.verify({
body,
headers: request.headers,
secret: process.env.MAILSET_HOOK_SECRET ?? '',
});
for (const event of events) {
if (event.type !== 'bounced') continue;
if (event.bounce.kind === 'hard')
console.log('Stop writing to', event.recipient);
}
return new Response(null, { status: 204 });
} catch (error) {
if (error instanceof MailsetError)
return new Response(error.code, { status: 401 });
throw error;
}
}
In the SDK’s tests: type-checked as a route handler.
Send from your own domain
Your product sends as your own domain either way. What changes is where your team’s mail lives.
- Sending only. Your team’s mail stays at Google, Microsoft or wherever it is now. Mailset asks for DKIM, a bounce host and DMARC, and leaves your MX alone. Send from a domain whose mail stays elsewhere
- Mailboxes too. Your team’s mail moves to Mailset, with a real mailbox for every address on the domain and your product’s email on the same bill. Set up a domain
mailset domains add example.com --sendingmailset domains apply example.com --sendingmailset domains check example.commailset keys create --send --domain example.com --from moc.elpmaxe@olleh --name "Acme API"What the TypeScript SDK’s build checks
Each line comes from the SDK’s own build, which fails when one stops being true.
| Fact | Detail | Known from |
|---|---|---|
| Node.js 20+ | Tested: a real send runs on it | A test |
| Bun 1.4+ | Tested: a real send runs on it | A test |
| Deno 2.9+ | Tested: a real send runs on it | A test |
| Cloudflare Workers | Compatibility date 2024-01-01, tested | A test |
| No dependencies | Nothing else to install beside it | Its manifest |
| Typed throughout | Types for every command and answer | A test |
| 30 KB | The whole SDK, minified, as one ES module | The build |
Where to go from here
- The TypeScript SDKEvery task, from the client’s options to errors and retries.
- Send email from Node.jsThe TypeScript guide, with no framework around it.
- WebhooksThe events Mailset posts about your product’s email, and how to check a request’s signature.
- Give sign-in codes a deadlineUse deliverBy so a code that would arrive late is never sent.
- Retry without sending twiceMake a send safe to repeat with an idempotency key or a Message-ID.
- Handle bouncesTell a dead address from a full inbox, and keep your list clean.
- Send many messages at onceUp to 100 messages a request, each with its own answer.
Product email on every plan
- Base$29a month10 members included
- Team$79a month25 members included
- Base
Regular price $29$14.50a month10 members included - Team
Regular price $79$39.50a month25 members included
Black Friday prices, for trials started by Thursday, Dec 31. Offer terms
Both include 10,000 emails a month, your product’s and your team’s together. Past that it’s $0.40 per 1,000 with pay as you go.
Sending from Next.js, asked and answered
Route handler or server action?
Either. Both run on the server, where MAILSET_KEY stays. Never import the client into a Client Component, which would put the key in the browser’s bundle.
Why request.text() and not request.json()?
The signature is over the raw bytes. Parsed and serialised again, the JSON changes in spacing or order and the check fails. Read the text; webhooks.verify parses the events for you.
Does it work on the Edge runtime?
The SDK needs only fetch and WebCrypto, which the Edge runtime has, and a real send is tested on Cloudflare Workers.
Where do the secrets go?
In server environment variables: MAILSET_KEY and MAILSET_HOOK_SECRET. Never give either a NEXT_PUBLIC_ prefix, which writes a variable into the browser bundle.
Make a key and sendTry it on your own domain
Make a key in Settings › Developers, keep it as MAILSET_KEY, and run the example above.7 days free, with everything on. Make a key, run the example above and watch the receipt come back.
By starting you agree to the Terms and Privacy Policy.