Frameworks

How to send email with Node.js

Short answer

To send email from Node.js, either use Nodemailer with an SMTP server or call an email API over HTTPS with fetch. An API is the better default for apps: one request per email, no connection pooling, and delivery events by webhook. Whichever you pick, verify your domain with SPF, DKIM and DMARC first or the mail will land in spam.

Last checked 16 November 2026

There are two ways to get an email out of a Node.js app: speak SMTP to a mail server, or make an HTTPS request to an email API. Both work. For app email such as sign-up confirmations, password resets and receipts, an API is simpler to run.

Option 1: fetch and an email API (no dependencies)

Node 18 and later ship fetch, so a single request sends an email. This example uses Mailcandor; most email APIs look almost the same.

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 <hello@acme.com>',
    to: 'sam@example.com',
    subject: 'Your login code',
    html: '<p>Your code is <b>481 203</b></p>',
  }),
});

if (!res.ok) {
  const { error } = await res.json();
  throw new Error(`${error.type}: ${error.message}`);
}
const { id } = await res.json(); // keep it to look the email up later

The API answers straight away with an id. Whether the email was delivered, bounced or marked as spam arrives later as a webhook.

To make retries safe, send an Idempotency-Key header with a value that is unique to the email, such as order-1042-receipt. If your code retries after a timeout, the second request returns the first response instead of sending a second email.

Option 2: Nodemailer over SMTP

If you already use Nodemailer, or a library that only speaks SMTP, point it at an SMTP relay.

import nodemailer from 'nodemailer';

const transport = nodemailer.createTransport({
  host: 'smtp.mailcandor.com',
  port: 465,
  secure: true,
  auth: { user: 'mailcandor', pass: process.env.MAILCANDOR_KEY },
});

await transport.sendMail({
  from: 'Acme <hello@acme.com>',
  to: 'sam@example.com',
  subject: 'Your login code',
  html: '<p>Your code is <b>481 203</b></p>',
});

Use port 465 with secure: true, or port 587 with secure: false so Nodemailer upgrades with STARTTLS.

Option 3: an existing SDK, pointed somewhere else

If your code already uses the Resend, SendGrid or Postmark SDK, you do not have to rewrite it to change provider. Mailcandor accepts their send calls as they are: see switching from Resend, from SendGrid or from Postmark.

Which should you choose?

Email API (fetch) SMTP (Nodemailer)
Dependencies None nodemailer
Speed per email One HTTPS request SMTP handshake, unless you pool connections
Delivery events Webhooks with an email id Usually none
Best for App email, serverless functions Libraries and tools that only speak SMTP

Before you send: verify your domain

Mail from a domain without SPF, DKIM and DMARC is likely to land in spam, whichever method you use. Add your domain in your provider’s dashboard and copy the DNS records it gives you. Our SPF, DKIM and DMARC guide explains each record.

Questions

Do I need Nodemailer to send email in Node.js?

No. Node 18 and later have fetch built in, so you can call an email API with no dependencies. Nodemailer is useful when you must speak SMTP.

Can I send email from Node.js with Gmail?

You can with an app password over SMTP, but Gmail limits how much you can send and is not meant for app email. Use a transactional email service for anything users rely on.

Why do my Node.js emails go to spam?

Usually because the sending domain has no SPF, DKIM or DMARC records, or because you send from a free address like gmail.com. Verify your own domain with your email provider first.

How do I send email from Node.js without blocking the request?

Send it from a background job or queue, or call the API and do not wait on delivery. The API answers in milliseconds with an id; delivery is reported later by webhook.

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.