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_KEYserver-only — never expose it viaNEXT_PUBLIC_. - Set
replyToto the visitor's address; keepfromon your verified domain. - Rate-limit the route, or bots will use your form to send you thousands of messages.