It is 2 AM, and PagerDuty triggers a red alert. A customer just completed an order, but 15 minutes have passed without receiving their activation email. Checking the logs reveals that the checkout endpoint crashed completely due to an unhandled syntax error: an unescaped double quote broke an 800-line HTML template literal string. Manually concatenating HTML strings in JavaScript was never a good idea at scale.
To solve this once and for all, we should offload email UI to React Email and handle delivery via the Resend API. Here is a production-ready implementation guide for Next.js App Router.
Quick Start (Deploy in 5 Minutes)
If you need to quickly set up a working email flow right away, follow these four steps.
1. Install Packages
Run the command to install the Resend SDK alongside React Email’s core components:
npm install resend @react-email/components
2. Configure Environment Variables
Create an account at resend.com, grab your API key, and paste it into your .env.local file:
RESEND_API_KEY=re_123456789_abcdefgHIJKLMN
3. Create an Email Template with React Components
Create an emails/WelcomeEmail.tsx file. You write it just like any regular React component:
import {
Html,
Head,
Body,
Container,
Text,
Button,
Heading,
Section
} from '@react-email/components';
import * as React from 'react';
interface WelcomeEmailProps {
userName: string;
activationUrl: string;
}
export const WelcomeEmail = ({
userName,
activationUrl,
}: WelcomeEmailProps) => {
return (
<Html>
<Head />
<Body style={{ backgroundColor: '#f6f9fc', fontFamily: 'sans-serif' }}>
<Container style={{ backgroundColor: '#ffffff', margin: '40px auto', padding: '24px', borderRadius: '8px' }}>
<Heading style={{ color: '#1a1a1a', fontSize: '20px' }}>
Welcome, {userName}!
</Heading>
<Text style={{ color: '#4a4a4a', fontSize: '15px', lineHeight: '24px' }}>
Your account is ready. Click the button below to verify your email and get started.
</Text>
<Section style={{ textAlign: 'center', marginTop: '24px', marginBottom: '24px' }}>
<Button
href={activationUrl}
style={{
backgroundColor: '#000000',
color: '#ffffff',
padding: '12px 24px',
borderRadius: '6px',
textDecoration: 'none',
fontWeight: 'bold',
}}
>
Verify Account
</Button>
</Section>
<Text style={{ color: '#8898aa', fontSize: '12px' }}>
If you did not sign up for this account, please ignore this email.
</Text>
</Container>
</Body>
</Html>
);
};
export default WelcomeEmail;
4. Write the Route Handler to Send Emails
Create the file app/api/send-email/route.ts in the App Router:
import { NextResponse } from 'next/server';
import { Resend } from 'resend';
import WelcomeEmail from '@/emails/WelcomeEmail';
const resend = new Resend(process.env.RESEND_API_KEY);
export async function POST(request: Request) {
try {
const { email, name } = await request.json();
const { data, error } = await resend.emails.send({
from: 'System <[email protected]>', // Default test address
to: [email],
subject: 'Verify your account',
react: WelcomeEmail({
userName: name,
activationUrl: 'https://itfromzero.com/verify?token=xyz123',
}),
});
if (error) {
return NextResponse.json({ error: error.message }, { status: 400 });
}
return NextResponse.json({ success: true, data });
} catch (err: any) {
return NextResponse.json({ error: err.message }, { status: 500 });
}
}
Open your terminal and test sending a request via cURL:
curl -X POST http://localhost:3000/api/send-email \
-H "Content-Type: application/json" \
-d '{"email": "[email protected]", "name": "John Doe"}'
The terminal returns a 200 OK status code along with the email ID (in the format re_xxxx). The email was dispatched successfully in under 300ms.
How It Works
How React Email and Resend Work Together
In the past, building transactional emails was painful. Legacy template engines like Handlebars or Pug forced you to juggle separate syntax. Even worse, email clients don’t follow modern web standards. Outlook on Windows still uses Microsoft Word’s rendering engine for HTML, causing modern CSS flexbox or grid layouts to break immediately.
React Email solves this at the build step. When you use components like <Container> or <Button>, the library automatically compiles them into deeply nested <table> tags and inlines all CSS. You write modern React syntax, but the output remains backwards-compatible all the way back to Outlook 2016.
Resend serves as the transport layer. Instead of manually configuring SMTP servers with Nodemailer, you only need to trigger an HTTPS API call. Resend directly accepts React elements, converts them into an HTML payload on the server, and distributes them to the recipient’s mail server.
Local Preview Without Consuming Credits
Don’t send real emails just to adjust 1–2 pixels of margin. React Email includes a standalone preview server. Add the following script to your package.json:
"scripts": {
"email:dev": "email dev -d ./emails -p 3001"
}
Install the preview CLI as a dev dependency:
npm install -D react-email
Run npm run email:dev and navigate to http://localhost:3001. You will instantly see a testing UI with hot-reload, letting you preview desktop and mobile layouts in real time.
Advanced Techniques
1. Using Tailwind CSS in Email Templates
You don’t have to write verbose inline styles. React Email provides a <Tailwind> component that allows you to use familiar Tailwind utility classes:
import {
Html,
Head,
Body,
Container,
Text,
Button,
Tailwind
} from '@react-email/components';
export const InvoiceEmail = ({ amount }: { amount: string }) => {
return (
<Html>
<Head />
<Tailwind>
<Body className="bg-gray-100 font-sans p-6">
<Container className="bg-white rounded-lg p-6 shadow-sm max-w-md mx-auto">
<Text className="text-xl font-bold text-gray-800">Payment Invoice</Text>
<Text className="text-gray-600 mt-2">
Amount paid: <strong className="text-black">{amount}</strong>
</Text>
<Button className="bg-blue-600 text-white font-medium px-4 py-2 rounded mt-4 inline-block">
Download PDF Invoice
</Button>
</Container>
</Body>
</Tailwind>
</Html>
);
};
2. Production DNS Configuration (Avoid the Spam Folder)
In development, Resend allows sending from [email protected], but only to your registered account email. When deploying to production, you must verify your own custom domain:
- Go to Resend Dashboard > Domains > Add Domain (using a subdomain like
mail.yourdomain.comornotify.yourdomain.comis recommended). - Resend will generate DNS records: MX (to receive feedback/inbound), TXT (SPF to authenticate sending servers), and CNAME records (DKIM for cryptographic signing).
- Add these exact records to your DNS provider (Cloudflare, Vercel DNS, or Route 53).
- Once the status shows Verified, you can start sending emails from
[email protected].
3. Listen to Webhooks to Monitor Bounce Rates
A successful API call does not guarantee the email reached the user’s inbox. Resend provides webhooks to notify you of delivery events: email.delivered, email.bounced, and email.complained.
Create a webhook handler at app/api/webhooks/resend/route.ts:
import { NextResponse } from 'next/server';
export async function POST(request: Request) {
const payload = await request.json();
const eventType = payload.type;
const email = payload.data?.to?.[0];
if (eventType === 'email.bounced') {
console.warn(`[Bounce Alert] Failed to deliver to: ${email}`);
// Mark account in database to suppress future emails
}
return NextResponse.json({ received: true });
}
Production Best Practices & Caveats
Here are several critical technical takeaways when operating an email system at scale:
- Mind the Rate Limits: Resend’s free tier is limited to 2 requests/second and 3,000 emails/month (up to 100 emails/day). If you are dispatching notifications to 5,000 users, never use
Promise.all. Doing so will immediately trigger HTTP 429 Too Many Requests errors. Instead, push tasks to a message queue like BullMQ or Upstash QStash to rate-limit and throttle outgoing traffic smoothly over time. - Image URL Rules: Email clients do not run on your local server. Never use relative paths like
/logo.png. All images must point to absolute HTTPS URLs (e.g.,https://cdn.yourdomain.com/logo.png). Always define explicitwidthandheightattributes to prevent layout shifts while assets load. - Always Include a Plain Text Fallback: Highly secure email clients (such as those in banking or government environments) and screen readers often disable HTML rendering. Always pass a plain text fallback via the
text: '...'field in your send call to ensure core content remains accessible. - Verify Webhook Signatures: When deploying webhooks in production, use the
svixlibrary to verify thesvix-signatureheader. This prevents attackers from forging fake webhook payloads against your server.

