General

How to Send Email in Next.js: Password Resets and Receipts via REST API or SMTP

Learn how to send password reset and receipt emails in Next.js with route handlers, server actions, REST or Nodemailer SMTP, plus SPF and DKIM setup.

November 18, 2026 · 5 min read

Want it fixed today? Free for 5,000 emails a month. No card.

Sign up with Google

To send email in Next.js, call your email provider from server-only code: a route handler or a server action. Use a REST API over HTTPS if you want it to work in any runtime, including edge. Use SMTP with Nodemailer if you prefer a familiar transport and run on the Node.js runtime. Either way, keep your credentials in server-only environment variables and send from a domain you have verified with SPF and DKIM.

This guide walks through both options, with code for a password reset email and a receipt email. SecureSMTP supports REST and SMTP, so the examples use it, but the pattern applies broadly.

REST API or SMTP: which should you choose?

Both get the message delivered. The difference is mostly about where your code runs.

REST API over HTTPS

A REST call is just a fetch request. It works in the Node.js runtime and in the edge runtime, and it needs no extra packages. For most Next.js apps, especially on serverless hosting, this is the simplest choice.

SMTP with Nodemailer

SMTP opens a TCP connection, so it needs the Node.js runtime. It will not run on the edge. It is a good fit if you are moving an existing app that already uses Nodemailer, because you mostly change the host and credentials. If you want to understand the relay side, see what an SMTP relay is.

  • Edge or unsure where it will run: use REST.
  • Existing Nodemailer code: use SMTP on the Node runtime.
  • New project on serverless: REST is usually less fuss.

Keep your keys on the server

Put your API key in .env.local without the NEXT_PUBLIC_ prefix. Anything with that prefix is exposed to the browser.

SECURESSMTP_API_KEY=qcs_live_...

The key is the site’s API key from the SecureSMTP dashboard (it starts with qcs_live_). Only read it in route handlers, server actions or other server modules. You do not set a From address here: emails go out from the sending domain you verified for the site, and you choose only the From name.

A shared helper

// lib/mail.ts
import 'server-only';

const API_URL = 'https://securessmtp.com/api/v1/mail/send';

export type MailMessage = {
  to: string | string[];
  subject: string;
  text?: string;
  html?: string;
  reply_to?: string | string[];
  from?: { name: string };
};

export async function sendMail(message: MailMessage) {
  const apiKey = process.env.SECURESSMTP_API_KEY;
  if (!apiKey) throw new Error('SECURESSMTP_API_KEY is not set');

  const res = await fetch(API_URL, {
    method: 'POST',
    headers: {
      'x-securessmtp-api-key': apiKey,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ from: { name: 'Your App' }, ...message }),
    signal: AbortSignal.timeout(30_000),
  });

  // Some failures come back as HTTP 200 with ok: false, so read the body, not only the status.
  const result = await res.json().catch(() => ({ ok: false }));
  if (!result.ok) {
    throw new Error(`SecureSMTP: ${result.reason ?? result.error ?? 'request_failed'} (HTTP ${res.status})`);
  }
  return result;
}

The server-only import makes the build fail if a client component imports this file by accident. The key goes in the x-securessmtp-api-key header, and from takes only a name. Always check ok in the response body: a blocked or over-limit send can answer HTTP 200 with "ok": false and a reason. The full helper, with types and error class, is in the Next.js guide.

Route handler example: password reset

// app/api/password-reset/route.ts
import { sendMail } from '@/lib/mail';

export async function POST(req: Request) {
  const { email } = await req.json();
  // createResetToken is your own code: make a single-use, expiring token and store a hash of it.
  const token = await createResetToken(email);
  const link = `${process.env.APP_URL}/reset?token=${token}`;

  try {
    await sendMail({
      to: email,
      subject: 'Reset your password',
      text: `Use this link to reset your password: ${link}\nIf you didn't ask for this, ignore this email.`,
    });
  } catch (err) {
    console.error('Password reset email failed:', err); // log it; don't tell the visitor why
  }

  // Same response whether or not the account exists.
  return Response.json({ ok: true });
}

Return the same response whether or not the address exists, so the form cannot be used to discover accounts.

Server action example: receipt

// app/checkout/actions.ts
'use server';
import { sendMail } from '@/lib/mail';

export async function sendReceipt(orderId: string) {
  const order = await getOrder(orderId); // getOrder is your own data layer
  await sendMail({
    to: order.email,
    subject: `Your receipt for order ${order.id}`,
    text: `Thanks for your order. Total: ${order.total}`,
  });
}

Call this after payment is confirmed, ideally from your payment webhook rather than the browser, so a closed tab never means a missing receipt. sendMail throws when the send fails, so let the webhook return an error and be retried.

The SMTP alternative with Nodemailer

// lib/smtp.ts
import nodemailer from 'nodemailer';

export const transport = nodemailer.createTransport({
  host: 'smtp.securessmtp.com',
  port: 587,
  secure: false, // STARTTLS on 587. For port 465 use: port: 465, secure: true
  auth: { user: 'securessmtp', pass: process.env.SECURESSMTP_API_KEY },
});

The username is always securessmtp and the password is the site’s API key. Then call transport.sendMail({ from, to, subject, text }) with a From address on your verified sending domain. In a route handler, add export const runtime = 'nodejs'; so it never lands on the edge.

Verify your domain: SPF and DKIM

Sending from your own domain only works well once the receiving servers can confirm you are allowed to. Add the DKIM record shown for your domain in the SecureSMTP dashboard (no SPF include is needed; Advanced mode adds an MX and an SPF record on a bounce subdomain), then wait for DNS to update. You can check your DKIM record with our DKIM validator.

Until the domain is verified, an unverified From address is the most common reason mail is rejected or lands in spam.

Check logs, webhooks and bounces

After sending, open the email log to see what happened to each message. For anything your app needs to react to, use webhooks rather than polling.

Handle bounces properly. SecureSMTP adds addresses that hard-bounce or complain to your suppression list, so they are not sent to again. Listen for the email.bounced and email.complained webhooks to update your own records. The deliverability checklist covers this in more depth.

Test it

  • Send to an inbox you control and confirm the From name and address.
  • Check the message headers for DKIM and SPF passes.
  • Look for the message in the email log.
  • Send to a made-up address on a domain you own, then check the bounce shows in the email log and reaches your webhook handler.
  • Check the reset link works once and then expires.

Common failures

  • Lands in spam: check SPF and DKIM, avoid link-only bodies, include a plain-text version.
  • Rejected From: the sending domain is not verified.
  • Works locally, fails deployed: environment variables are missing on the host.
  • SMTP fails on edge: switch to REST or set the Node runtime.

Next step

SecureSMTP includes 5,000 free emails a month, enough to build and test this flow. The Next.js guide has the full helper, a contact form with a Server Action and the error codes, and POST /mail/send lists every field. Contact support if something does not behave as expected.

Related reading

Fix email delivery on your site

SecureSMTP sends WordPress, Shopify and app email through an authenticated mail service, DKIM-signed for your own domain, and shows the result of every email.

Free plan: 5,000 emails a month. No card needed. Setup takes a few minutes, plus DNS time for your sending domain.

Send your site’s email through SecureSMTP. Free for 5,000 emails a month. No card needed.