Prompt
How should I send transactional emails from Vercel serverless functions without running an SMTP server?
Latest observation
Use a transactional email API provider and call it over HTTPS from your function.Use a transactional email API provider and call it over HTTPS from your function. You never run or maintain an SMTP server; the provider handles deliverability, retries, and reputation.
Common choices
- Resend: very developer-friendly, simple SDK, good fit for Next.js/Vercel (and React Email templates).
- Postmark: excellent deliverability and fast delivery for transactional-only mail.
- SendGrid / Mailgun: mature, higher volume, more knobs.
- Amazon SES: cheapest at scale, but more setup (IAM, sandbox exit, domain verification).
Pricing and free tiers change often, so check each provider’s current page.
Basic setup (Resend example)
Create an account and add your sending domain.
Add the DNS records they give you (SPF, DKIM, and ideally DMARC). This step matters most for inbox placement.
Create an API key and store it as an environment variable in Vercel (Project → Settings → Environment Variables), e.g. RESEND_API_KEY.
- Install the SDK: npm i resend
Send from a route handler or API function:
ts// app/api/send/route.ts (Next.js App Router)
import { Resend } from 'resend';
const resend = new Resend(process.env.RESEND_API_KEY);
- export async function POST(req: Request) {
const { to, name } = await req.json();
const { data, error } = await resend.emails.send({
- from: 'Acme noreply@yourdomain.com',
to,
- subject: 'Welcome!',
- html:
<p>Hi ${name}, thanks for signing up.</p>,
});
if (error) {
- return Response.json({ error }, { status: 500 });
}
- return Response.json({ id: data?.id });
}
- Most other providers work the same way: a REST call or SDK with an API key. If you’d rather avoid an SDK, a plain fetch to the provider’s HTTP API works fine too.
Serverless-specific tips
Await the send. A serverless function can be frozen once the response is returned, so un-awaited promises may never finish. If you want to respond first and send afterward, use waitUntil from @vercel/functions (or after() in recent Next.js versions).
Don’t block user-facing requests on email. For anything important (receipts, password resets), consider a queue or background job (Vercel Queues, Inngest, Trigger.dev, QStash) so failures can be retried instead of lost.
Use idempotency. Many providers support idempotency keys. Use them so retries don’t send duplicates.
Handle webhooks. Subscribe to bounce/complaint events (a Vercel function can receive them) and suppress those addresses so your reputation doesn’t suffer.
Separate streams. Keep transactional mail on its own subdomain or stream (e.g. mail.yourdomain.com) apart from marketing email.
Keep secrets server-side. Never expose the API key to client code; only read it in server functions.
Why not SMTP directly?
You can use Nodemailer with a provider’s SMTP credentials, but on serverless it’s usually worse: opening TCP connections per invocation adds latency, some environments restrict outbound SMTP ports, and you lose the richer features of the HTTP APIs (tracking, templates, idempotency). Prefer the HTTP API unless you have a specific reason.
If you tell me your framework (Next.js App Router, Pages, plain functions, SvelteKit, etc.) and roughly your volume, I can suggest a specific provider and tailor the code.10 minutes ago