Prompt
How do I send emails from a Vercel app?
Latest observation
You have a few solid options for sending email from a Vercel app. The most common approach is:
- Use a server-side API route or server action
- Call an email provider’s API
- Never send email directly from client-side code
Recommended approach
1) Pick an email service
Common choices:
- Resend — very popular for modern apps and easy with Vercel
- SendGrid
- Postmark
- Mailgun
- Amazon SES
If you want the simplest setup, Resend is a great default.
Example: sending email with Resend in a Next.js app on Vercel
Install
npm install resend
Add your API key to Vercel environment variables
In Vercel dashboard:
- Project → Settings → Environment Variables
- Add:
RESEND_API_KEY=...
For local development, put it in .env.local.
Create an API route
If you're using Next.js App Router:
// app/api/send-email/route.ts
import { NextResponse } from "next/server";
import { Resend } from "resend";
const resend = new Resend(process.env.RESEND_API_KEY!);
export async function POST(req: Request) {
const { to, subject, text } = await req.json();
try {
const data = await resend.emails.send({
from: "Your App <noreply@yourdomain.com>",
to,
subject,
text,
});
return NextResponse.json({ success: true, data });
} catch (error) {
return NextResponse.json(
{ success: false, error: "Failed to send email" },
{ status: 500 }
);
}
}
Call it from the client
await fetch("/api/send-email", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
to: "user@example.com",
subject: "Welcome!",
text: "Thanks for signing up.",
}),
});
Important Vercel notes
Use serverless/server-side only
Do not expose your email API key in client-side code.
Verify your sender domain
Most providers require you to:
- verify your domain
- set up SPF/DKIM records
- use a real
fromaddress likenoreply@yourdomain.com
Watch execution time
Vercel serverless functions have limits, so email sending should be quick. If you need retries or heavy processing, use a background job/queue.
If you just want a contact form
For a simple “contact us” form, create a server endpoint that:
- validates input
- rate limits requests
- sends email to your inbox
Alternative: use a server action
If you’re using Next.js Server Actions, you can send email directly in the action instead of an API route, but the same rule applies: the code must run server-side.
Best practices
- Validate form input
- Add spam protection (honeypot, CAPTCHA, rate limiting)
- Handle errors gracefully
- Keep API keys in environment variables
- Use a verified sending domain
If you want, I can give you:
- a Next.js + Resend full working example,
- a contact form implementation,
- or examples using SendGrid/Postmark/Mailgun.