Next.js Email Testing with Resend & Mock SMTP

Next.js & React Developers Intermediate 9 min read Next.js email testing
TL;DR

Use Nodemailer in Next.js API routes or server actions to send emails to Mockmail’s hosted mock SMTP. Messages are captured (not delivered) so you can verify HTML, text, headers, and attachments in the web UI. Keep Resend for real delivery in staging/production; use a simple feature flag to switch.

Quick Start

  1. Create an inbox at mockmail.io and copy its SMTP settings (host, port, username, password, encryption).
  2. Add those values to your Next.js project as environment variables.
  3. Use Nodemailer from an API route or a server action to send to Mockmail.

Environment Variables

Create .env.local (values come from your Mockmail inbox page):

SMTP_HOST=your-mockmail-host
SMTP_PORT=25
SMTP_USER=your-username
SMTP_PASS=your-password
SMTP_SECURE=false
APP_FROM_EMAIL=noreply@example.test

Install Nodemailer:

npm i nodemailer

API Route (Nodemailer → Mockmail)

app/api/send/route.ts

import { NextResponse } from 'next/server';
import nodemailer from 'nodemailer';

export const runtime = 'nodejs'; // SMTP requires Node.js runtime (not Edge)

const transporter = nodemailer.createTransport({
    host: process.env.SMTP_HOST!,
    port: Number(process.env.SMTP_PORT || '25'),
    secure: false,
    auth: process.env.SMTP_USER
        ? { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS }
        : undefined,
});

export async function POST(req: Request) {
    const {
        to = 'dev@example.test',
        subject = 'Hello from Next.js',
        html = '<p>Test</p>',
    } = await req.json();

    try {
        const info = await transporter.sendMail({
            from: process.env.APP_FROM_EMAIL || 'noreply@example.test',
            to,
            subject,
            html,
            text: 'Test',
        });
        return NextResponse.json({ ok: true, messageId: info.messageId });
    } catch (e: any) {
        return NextResponse.json({ ok: false, error: e?.message }, { status: 500 });
    }
}

Test it:

curl -X POST http://localhost:3000/api/send \
  -H "content-type: application/json" \
  -d '{"to":"dev@example.test","subject":"Dev test","html":"<h1>Hello</h1>"}'

Server Action

app/actions.ts

'use server';

import nodemailer from 'nodemailer';

export const runtime = 'nodejs';

const transporter = nodemailer.createTransport({
    host: process.env.SMTP_HOST!,
    port: Number(process.env.SMTP_PORT || '25'),
    secure: process.env.SMTP_SECURE === 'true',
    auth: process.env.SMTP_USER
        ? { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS }
        : undefined,
});

export async function sendWelcomeEmail(email: string) {
    await transporter.sendMail({
        from: process.env.APP_FROM_EMAIL || 'noreply@example.test',
        to: email,
        subject: 'Welcome!',
        html: '<p>Welcome to our app.</p>',
        text: 'Welcome to our app.',
    });
    return { ok: true };
}

Call it from your UI via a form or handler.


Using Resend (Toggle)

Resend is API-based and does not deliver through Mockmail. For safe dev testing use Nodemailer → Mockmail; enable Resend only when you want real delivery.

const useResend = process.env.USE_RESEND === 'true';

if (useResend) {
    // Use your real provider (e.g., Resend API)
    // const resend = new Resend(process.env.RESEND_API_KEY!);
    // await resend.emails.send({ from, to, subject, html });
} else {
    // Use Nodemailer -> Mockmail (see examples above)
}

Inspect & Share

Open your Mockmail inbox to:

  • View HTML and Text versions
  • Inspect headers (From/To/Reply-To, custom headers)
  • Download attachments
  • Share a captured email via a unique link with your team

Note: Because emails are captured by a mock SMTP, SPF/DKIM/DMARC don’t apply here. Handle deliverability after switching to your real provider.


Troubleshooting

  • No email in Mockmail? Recheck host/port/encryption/credentials against the inbox settings.
  • Edge runtime error? Add export const runtime = 'nodejs'.
  • Env not loaded? Confirm .env.local locally and variables in Vercel for deployments.
  • Provider blocks SMTP? Use an allowed port from Mockmail or send from a server that permits outbound SMTP.

Frequently Asked Questions

Can I test Resend emails in Next.js?

Yes. Configure Resend to use Mockmail's SMTP endpoint for testing, or use Mockmail's hosted service as a drop-in replacement.

Does this work with Next.js 13+ server actions?

Yes. Server actions can send email via SMTP just like API routes.

Start testing emails safely

Use Mockmail for free to test and debug emails with zero risk.

Register