Frameworks

How to send email with Nuxt

Short answer

To send email from a Nuxt app, create a server route such as server/api/contact.post.ts, keep the API key in runtimeConfig (set by the NUXT_MAILCANDOR_KEY environment variable), and call the email API with $fetch. The browser posts the form to your route and never sees the key. Nuxt's server runs on Nitro, so this works on Node hosts and on serverless and edge platforms.

Last checked 16 November 2026

To send email from Nuxt, add a server route under server/api/, keep your email API key in runtimeConfig, and call the API with $fetch. The Vue page posts to your route; the route sends the email. The key stays on the server, and because Nuxt’s server is built on Nitro, the same code deploys to Node servers, serverless functions and edge platforms.

This guide uses Nuxt 4 and sends through Mailcandor. Other email APIs work the same way with a different URL and body.

Step 1: add the key to runtimeConfig

// nuxt.config.ts
export default defineNuxtConfig({
  runtimeConfig: {
    mailcandorKey: '', // server only: never put this under `public`
  },
})

Then set the value with an environment variable. Nuxt maps NUXT_ plus the key in upper snake case onto runtimeConfig at runtime:

# .env
NUXT_MAILCANDOR_KEY=mc_live_...

Keys at the top level of runtimeConfig are only available on the server. Anything under runtimeConfig.public is sent to the browser, so the key must never go there.

Step 2: create the server route

The .post.ts suffix makes the route answer only POST requests.

// server/api/contact.post.ts
export default defineEventHandler(async (event) => {
  const { name, email, message } = await readBody(event)

  if (typeof email !== 'string' || !email.includes('@') || typeof message !== 'string' || !message.trim()) {
    throw createError({ statusCode: 400, statusMessage: 'Please fill in your email and message.' })
  }

  const { mailcandorKey } = useRuntimeConfig(event)

  try {
    const { id } = await $fetch<{ id: string; status: string }>(
      'https://api.mailcandor.com/v1/emails',
      {
        method: 'POST',
        headers: { Authorization: `Bearer ${mailcandorKey}` },
        body: {
          from: 'Acme website <hello@acme.com>',
          to: 'team@acme.com',
          reply_to: email,
          subject: `Contact form: ${String(name || email).slice(0, 80)}`,
          text: message,
        },
      },
    )
    return { ok: true, id }
  } catch (err: any) {
    console.error('email failed', err?.data?.error)
    throw createError({ statusCode: 502, statusMessage: 'Could not send your message.' })
  }
})

Points worth noting:

  • $fetch serializes a plain object body as JSON and sets the content type, so there is no JSON.stringify.
  • $fetch throws on any 4xx or 5xx response. The API’s error details are on err.data.error, with type, message and field. Log them; do not show them to visitors.
  • Passing event to useRuntimeConfig lets environment variables override the config at runtime in server routes, as the Nuxt server docs recommend.
  • The email goes to your own team with the visitor in reply_to, so the form cannot be used to send mail to anyone else.

Step 3: call the route from a page

<!-- app/pages/contact.vue -->
<script setup lang="ts">
const form = reactive({ name: '', email: '', message: '' })
const status = ref<'idle' | 'sending' | 'sent' | 'error'>('idle')

async function submit() {
  status.value = 'sending'
  try {
    await $fetch('/api/contact', { method: 'POST', body: form })
    status.value = 'sent'
  } catch {
    status.value = 'error'
  }
}
</script>

<template>
  <form @submit.prevent="submit">
    <input v-model="form.name" name="name" placeholder="Name">
    <input v-model="form.email" name="email" type="email" required placeholder="Email">
    <textarea v-model="form.message" name="message" required />
    <button :disabled="status === 'sending'">Send</button>
    <p v-if="status === 'sent'">Thanks, we will reply soon.</p>
    <p v-if="status === 'error'">Something went wrong. Please try again.</p>
  </form>
</template>

Before going live, protect the route from bots. A hidden honeypot field that real users leave empty stops simple scripts; a CAPTCHA such as Cloudflare Turnstile stops more.

Transactional email from Nuxt

The same $fetch call sends sign-up confirmations, receipts and alerts from any server route or server utility. Two habits pay off:

  • Use an Idempotency-Key header with a value unique to the email, for example order-${orderId}-receipt. If the route runs twice, the second call returns the first response instead of sending a duplicate.
  • Do not make users wait on email. The API answers quickly with a queued status, but on serverless presets you can also use event.waitUntil(promise) to send after the response.

For many recipients at once, POST /v1/emails/batch takes up to 100 emails per request.

Receiving delivery events

To learn when an email bounces or is marked as spam, add a second route, such as server/api/email-events.post.ts, and register its URL as a webhook endpoint with your email provider. Read the body with readRawBody(event) rather than readBody, because signature checks need the exact bytes that were sent. Mailcandor signs webhooks with the Standard Webhooks scheme, so the standardwebhooks npm package can verify them with your endpoint secret. Reject anything that fails the check, answer with a 2xx quickly, and do any slow work afterwards. Failed deliveries are retried for about 28 hours, so a short outage does not lose events.

Can I use SMTP and Nodemailer?

On a Node.js host (the default node-server preset), Nodemailer works in a server route exactly as in the Node.js email guide. On edge presets such as Cloudflare it will not, because those runtimes do not provide Node’s SMTP networking; see sending from Cloudflare Workers. An email API over HTTPS works on every Nitro preset, which keeps your deploy target open.

Before you send: verify your domain

Mail from a domain without SPF, DKIM and DMARC records is likely to be filtered as spam. Add your domain with your email provider and publish the records it gives you. The SPF, DKIM and DMARC setup guide explains each record.

Sending with Mailcandor

The route above works with Mailcandor’s email API as written; there is no SDK or Nuxt module to install. Access is by request through the pricing page, and the free Starter plan covers 3,000 emails a month.

Questions

Can I send email from the Vue frontend directly?

No. Sending needs a secret API key or SMTP password, which would be visible to anyone in browser code. Post the form to a Nuxt server route and send from there.

How do I keep my email API key secret in Nuxt?

Declare it as a top-level key in runtimeConfig, not under public, and set it with an environment variable such as NUXT_MAILCANDOR_KEY. Read it in server routes with useRuntimeConfig(event).

Can I use Nodemailer in Nuxt?

Yes, in server routes when you deploy to a Node.js host. It will not work on edge presets such as Cloudflare Workers, which cannot use Node's SMTP stack. An email API works on every preset.

Is there a Nuxt module for email?

There are community modules, but you do not need one. A server route and $fetch, both built into Nuxt, are enough to call an email API.

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.