Cloudflare Workers

Send email from Cloudflare Workers

The SDK needs fetch and WebCrypto, which a Worker has, so it sends from the edge as it does from a server. The key comes from env, and the webhook reads request.text().

Open Mailset
Continue with Google

By starting you agree to the Terms and Privacy Policy.

Install

Install the TypeScript SDK

Add mailset to the Worker’s package.json, and the bundler takes it in as it is, with nothing else to pull.

npm install mailset
Send

Send your first email from Cloudflare Workers

A Worker’s secrets arrive on env, not process.env, so fetch makes the client with new Mailset({ key: env.MAILSET_KEY }). It reads the email from the request’s JSON, sends with deliverBy: '10m', and answers the receipt’s id with Response.json.

src/index.ts
import { Mailset } from 'mailset';

type Env = { MAILSET_KEY: string };

export default {
	async fetch(request: Request, env: Env) {
		// A Worker's secrets are in env, not process.env.
		const mailset = new Mailset({ key: env.MAILSET_KEY });
		const { email } = (await request.json()) as {
			email: string;
		};
		const sent = await mailset.send({
			from: 'Acme <moc.elpmaxe@olleh>',
			to: email,
			subject: 'Your Acme code',
			text: 'Your code is 123456.',
			deliverBy: '10m',
		});
		return Response.json({ id: sent.id });
	},
};

In the SDK’s tests: run in workerd through Miniflare.

Webhooks

Verify webhooks in a Worker

This Worker reads request.text() before anything parses it, then passes the text, request.headers and env.MAILSET_HOOK_SECRET to webhooks.verify. A MailsetError answers 401 with its code, any other error is thrown, and a Response with status 204 says the events are kept.

src/index.ts
import { MailsetError, webhooks } from 'mailset';

type Env = { MAILSET_HOOK_SECRET: string };

export default {
	async fetch(request: Request, env: Env) {
		// The signature covers the body as it came: read
		// it as text, never request.json().
		const body = await request.text();
		try {
			const events = await webhooks.verify({
				body,
				headers: request.headers,
				secret: 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)) throw error;
			return new Response(error.code, { status: 401 });
		}
	},
};

In the SDK’s tests: run in workerd through Miniflare.

Domain

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"
Facts

What the TypeScript SDK’s build checks

Each line comes from the SDK’s own build, which fails when one stops being true.

FactDetailKnown from
Node.js 20+Tested: a real send runs on itA test
Bun 1.4+Tested: a real send runs on itA test
Deno 2.9+Tested: a real send runs on itA test
Cloudflare WorkersCompatibility date 2024-01-01, testedA test
No dependenciesNothing else to install beside itIts manifest
Typed throughoutTypes for every command and answerA test
30 KBThe whole SDK, minified, as one ES moduleThe build
Next

Where to go from here

Pricing

Product email on every plan

  • Base$29a month10 members included
  • Team$79a month25 members included
  • BaseRegular price $29$14.50a month10 members included
  • TeamRegular 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.

Every price, and what teams pay elsewhere

Questions

Sending from Cloudflare Workers, asked and answered

Where does the key go?

In the Worker’s secrets: wrangler secret put MAILSET_KEY, then read it as env.MAILSET_KEY. MAILSET_HOOK_SECRET goes the same way.

Does it need the nodejs_compat flag?

No. The SDK uses fetch and WebCrypto alone, and these Workers run in workerd with a compatibility date of 2024-01-01 and no flags.

Why make the client inside fetch?

Because env arrives with each request. Making a client costs no network call: nothing is sent until send runs.

Is the SDK tested on Workers?

Yes. Both of these Workers run in workerd through Miniflare in the SDK’s tests, and a real send runs there too.

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.

Open Mailset
Continue with Google

By starting you agree to the Terms and Privacy Policy.