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.