Tutorial

Send Email from Next.js (App Router & Route Handlers)

The App Router way: a Server Action calling the REST API directly, no SMTP client needed.

By the Sendcorex team·Updated Sep 21, 2026·8 min read

Why REST over SMTP here

Most Next.js deployments run on serverless or edge functions where long-lived outbound SMTP connections are unreliable or blocked outright. A single HTTPS POST from a Server Action avoids the problem entirely.

A Server Action for a contact form

// app/actions.ts
'use server';

export async function sendContactMessage(formData: FormData) {
  const email = formData.get('email') as string;
  const message = formData.get('message') as string;

  const res = await fetch('https://graph.sendcorex.com/v3.0/emails', {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${process.env.SENDCOREX_API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      to: 'you@yourdomain.com',
      from: 'hello@yourdomain.com',
      replyTo: email,
      subject: 'New contact message',
      text: message,
    }),
  });

  if (!res.ok) throw new Error('Could not send message');
}

Or a Route Handler, if you'd rather call it from the client

// app/api/contact/route.ts
export async function POST(request: Request) {
  const { email, message } = await request.json();

  const res = await fetch('https://graph.sendcorex.com/v3.0/emails', {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${process.env.SENDCOREX_API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ to: 'you@yourdomain.com', from: 'hello@yourdomain.com', replyTo: email, subject: 'New contact message', text: message }),
  });

  return Response.json({ ok: res.ok }, { status: res.ok ? 200 : 502 });
}

Rendering with React Email

For anything richer than a plain string, render a React Email component to HTML with @react-email/render and pass the result as html in the request body — keeps templates as components instead of string concatenation.

Checklist

  • Keep SENDCOREX_API_KEY server-only — never expose it via NEXT_PUBLIC_.
  • Set replyTo to the visitor's address; keep from on your verified domain.
  • Rate-limit the route, or bots will use your form to send you thousands of messages.

Send your first email in minutes

Create a free account, verify your domain and paste in the code above.

No credit card required