NestJS

Send email from NestJS

A MailService provider holds the client, a controller sends through it, and the webhook controller verifies req.rawBody. Nest keeps that raw body only when the app is created with rawBody: true.

Open Mailset
Continue with Google

By starting you agree to the Terms and Privacy Policy.

Install

Install the TypeScript SDK

mailset from npm, pnpm, Bun or Yarn, next to @nestjs/common. Its types ship inside the package, so the client type-checks with your decorators and DTOs.

npm install mailset
Send

Send your first email from NestJS

MailService is @Injectable(), so Nest makes one for the app, and its client reads MAILSET_KEY. CodesController takes the service in its constructor and calls sendCode with the posted email; deliverBy: '10m' refuses a code that would come late, and the route answers the receipt’s id.

codes.module.ts
import {
	Body,
	Controller,
	// A provider: Nest makes one for the app.
	Injectable,
	Module,
	Post,
} from '@nestjs/common';
import { Mailset } from 'mailset';

@Injectable()
export class MailService {
	// One client for the app. Reads MAILSET_KEY.
	private readonly mailset = new Mailset();

	sendCode(email: string) {
		return this.mailset.send({
			from: 'Acme <moc.elpmaxe@olleh>',

In the SDK’s tests: run on NestJS 11, type-checked by tsc.

Webhooks

Verify webhooks from req.rawBody

EventsController reads req.rawBody through RawBodyRequest<Request>, since @Body() would be parsed JSON. bootstrap creates the app with rawBody: true, without which req.rawBody is undefined. A failed check throws UnauthorizedException with the error’s code, and @HttpCode(204) answers a batch that went through.

events.module.ts
import {
	Controller,
	HttpCode,
	Module,
	Post,
	type RawBodyRequest,
	Req,
	UnauthorizedException,
} from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import type { Request } from 'express';
import { MailsetError, webhooks } from 'mailset';

const secret = process.env.MAILSET_HOOK_SECRET ?? '';

@Controller('mailset')
export class EventsController {
	@Post('events')

In the SDK’s tests: run on NestJS 11, type-checked by tsc.

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 NestJS, asked and answered

Why rawBody: true?

Nest parses JSON bodies before a controller runs. With rawBody: true in NestFactory.create, it also keeps the bytes as req.rawBody, which is what the signature covers.

Why not @Body() for the webhook?

@Body() hands over the parsed object, and the bytes it came from are gone. Verify req.rawBody instead; webhooks.verify returns the events typed.

Should the client be a provider?

Yes. As an @Injectable() service it is made once and injected wherever a controller or another provider asks for it.

What does a bad signature get back?

UnauthorizedException(error.code) makes Nest answer 401 with the reason. Mailset posts that batch again later, so a fixed secret catches up.

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.