Frameworks

How to send email from Vercel Functions

Short answer

Vercel does not send email itself, so you send it from a Vercel Function: store your email API key as an environment variable, then POST to the email API with fetch from a file in /api or a Next.js route handler. An API suits serverless well because each email is one short HTTPS request with no SMTP connection to set up. Use waitUntil or after to send without delaying the response.

Last checked 16 November 2026

Vercel runs your code but does not send email, so you send it from a Vercel Function. Store an email API key as an environment variable, then fetch the email API from a file in /api or from a Next.js route handler. Each email is one HTTPS request, which fits serverless functions far better than opening an SMTP connection on every cold start.

Step 1: add your API key as an environment variable

In the Vercel dashboard, open your project, then Settings, Environment Variables, and add MAILCANDOR_KEY. Or from the CLI:

vercel env add MAILCANDOR_KEY

Pull it into a local .env file for development with vercel env pull. Read it in server code with process.env.MAILCANDOR_KEY. In Next.js, never prefix it with NEXT_PUBLIC_; that prefix inlines the value into browser JavaScript, where anyone can read it.

Step 2a: a Vercel Function without a framework

Any file in /api becomes a function. Vercel Functions use web standard handlers, so you export a function per HTTP method that takes a Request and returns a Response. The example sends through Mailcandor; other APIs look much the same.

// api/contact.ts
export async function POST(request: Request) {
  const { email, message } = await request.json();
  if (typeof email !== 'string' || typeof message !== 'string' || !message.trim()) {
    return Response.json({ error: 'Missing fields' }, { status: 400 });
  }

  const res = await fetch('https://api.mailcandor.com/v1/emails', {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${process.env.MAILCANDOR_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      from: 'Acme website <hello@acme.com>',
      to: 'team@acme.com',
      reply_to: email,
      subject: 'New contact form message',
      text: message,
    }),
  });

  if (!res.ok) {
    const { error } = await res.json();
    console.error('email failed', error.type, error.message);
    return Response.json({ error: 'Could not send' }, { status: 502 });
  }
  return Response.json({ ok: true });
}

Outside a framework, Vercel needs "type": "module" in package.json (or a .mjs extension for JavaScript) for this syntax. Node 18 and later have fetch built in, so there is nothing to install.

Two choices in this code are deliberate. The email goes to your own address with the visitor in reply_to, so the form cannot be used to send mail to strangers. And the error from the API is logged but not shown to the visitor.

Step 2b: a Next.js route handler

In the App Router, the same code lives in app/api/contact/route.ts. Route handlers export the same POST(request: Request) function, and Vercel deploys each one as a function. Server Actions work too: call the same fetch inside a function marked 'use server'.

Send without making the user wait

The API answers quickly with {"id": "...", "status": "queued"}, but you can still return the response first and send afterwards. In a serverless function you must tell the platform to keep running, or the work may be cut off once the response is sent.

Outside Next.js, use waitUntil from the @vercel/functions package:

import { waitUntil } from '@vercel/functions';

export async function POST(request: Request) {
  const { email } = await request.json();
  waitUntil(sendWelcomeEmail(email)); // runs after the response
  return Response.json({ ok: true });
}

In Next.js, use after from next/server, which does the same job inside route handlers and Server Actions. Errors in work done this way do not reach the user, so log them.

Make retries safe

Serverless requests get retried: by users clicking twice, by your own retry logic, by a queue. Add an Idempotency-Key header with a value unique to the email, such as signup-${userId}-welcome. A repeated key returns the first response instead of sending a second email.

For several emails at once, such as notifying every member of a team, POST /v1/emails/batch accepts up to 100 emails in one request (without attachments or scheduling). That keeps you well inside both the function’s time limit and your API rate limit.

What about SMTP and Nodemailer?

Nodemailer works in Node.js Vercel Functions; the Node.js guide shows the setup with port 465 or 587. It is a weaker fit here: every cold start opens a new SMTP connection and TLS handshake before the email can go, and connection pooling does little when instances come and go. You also lose the email id that an API returns, which you need to match delivery webhooks to the email that caused them. The SMTP vs API guide covers this in more depth.

Receiving delivery events on Vercel

To know when an email bounces or is marked as spam, add a second function, such as api/email-events.ts, and register its URL as a webhook endpoint with your provider. Verify the signature on every request before trusting it. Mailcandor signs webhooks with the Standard Webhooks scheme, so the standardwebhooks npm package or a few lines of HMAC code can check them. Answer with a 2xx quickly; failed deliveries are retried for about 28 hours.

Before you send: verify your domain

Whatever you deploy, mail from a domain without SPF, DKIM and DMARC records is likely to land in spam. Add your sending domain in your email provider’s dashboard and publish its records with whoever runs your DNS, which may be Vercel if your domain uses Vercel’s nameservers. The SPF, DKIM and DMARC setup guide explains each record.

Sending with Mailcandor

The code above works with Mailcandor’s email API as written; there is no SDK package to install. If your code already uses the Resend SDK, you can point it at Mailcandor by setting RESEND_BASE_URL=https://api.mailcandor.com/v1 instead of rewriting it. Access is by request through the pricing page.

Questions

Does Vercel have an email service?

No. Vercel hosts and runs your code. To send email you call an email API or an SMTP relay from a Vercel Function.

Can I use Nodemailer on Vercel?

Nodemailer runs in Node.js functions, but each cold start pays for a new SMTP connection and handshake. An HTTPS email API is usually faster and simpler in serverless code.

Where do I put my email API key on Vercel?

In Project Settings, Environment Variables, or with vercel env add. Read it with process.env in server code and never prefix it with NEXT_PUBLIC_, which would expose it to the browser.

How do I send email after the response in a Vercel Function?

Use waitUntil from @vercel/functions, or after from next/server in Next.js. Both keep the function alive until the email request finishes.

Related

Send with someone on your side.

Unlimited domains on every plan, a person before any block, and 3,000 emails a month free. Tell us what you send and we set you up within one business day.