Send email from Next.js with Lettr
Lettr works with Next.js through the lettr package on npm, from Route Handlers and Server Actions.
Checked
What you can do
- Send transactional email from a Route Handler or a Server Action
- Send a contact form straight from a Server Action, with no API route
- Send HTML you render yourself, React Email included, or a template from the Lettr editor
- Keep the API key on the server, in .env.local or your Vercel environment variables
- Receive delivery and bounce webhooks in a Route Handler
How to set it up
npm install lettrOn Vercel, add the same variable under Settings, then Environment Variables.
LETTR_API_KEY=lttr_your_api_keyA Route Handler runs on the server, so the key never reaches the browser.
import { NextResponse } from "next/server";
import { Lettr } from "lettr";
const lettr = new Lettr(process.env.LETTR_API_KEY!);
export async function POST(request: Request) {
const { to, subject, html } = await request.json();
const { data, error } = await lettr.emails.send({
from: "hello@yourdomain.com",
to: [to],
subject,
html,
});
if (error) return NextResponse.json({ error: error.message }, { status: 500 });
return NextResponse.json(data);
}Or skip the route: pass this Server Action to a form's action prop.
"use server";
import { Lettr } from "lettr";
const lettr = new Lettr(process.env.LETTR_API_KEY!);
export async function sendContact(formData: FormData) {
await lettr.emails.send({
from: "contact@yourdomain.com",
to: ["team@yourdomain.com"],
subject: `Message from ${formData.get("name")}`,
text: String(formData.get("message")),
});
}Requirements
- Node.js 18 or later
- A Next.js app (App Router or Pages Router)
- A sending domain verified in Lettr
Links
Questions
- What's the best way to send email from Next.js?
- Call an email API from server code: a Route Handler or a Server Action. With Lettr that's the lettr package from npm and one emails.send call. Never send from a Client Component, because the API key has to stay on the server.
- Can I send email from a Next.js Server Action?
- Yes. Mark the file with "use server", call lettr.emails.send inside the action and pass the action to a form. A contact form then sends without a separate API route.
- Does it work on Vercel?
- Yes. Route Handlers and Server Actions run as Vercel Functions on the Node.js runtime with the lettr package; add LETTR_API_KEY in your Vercel environment variables. On the Edge runtime, the docs call the REST API with fetch instead.
- Can I use React Email templates?
- Yes. Render the component to HTML with @react-email/render and send it as html. If your team wants to edit emails without a deploy, build them in the Lettr editor and send them by template instead.
- Does it work with the Pages Router?
- Yes. Call the same lettr.emails.send from an API route in pages/api. The SDK only needs server-side Node.js.
- Can I test before verifying a domain?
- Yes. Create a sandbox API key: every email goes to your own inbox from a pre-verified sandbox domain, with no DNS setup.