Skip to content

Send Email with Node.js

  1. Sign up for an email delivery service and generate an API key
  2. Verify your sending domain with SPF and DKIM records
  3. Choose your sending method — REST API (fetch) or SMTP (Nodemailer)
  4. Build the email payload with sender, recipient, subject, and HTML body
  5. Send the email and handle the response
  6. Set up webhooks to track delivery, bounces, and opens

Node.js 18+ includes the built-in fetch API. No dependencies required.

const response = await fetch("https://api.relaypost.dev/v1/emails/send", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.RELAYPOST_API_KEY}`,
},
body: JSON.stringify({
from: { email: "[email protected]", name: "Your App" },
to: [{ email: "[email protected]" }],
subject: "Your order has shipped",
html: "<h1>Order Shipped</h1><p>Your package is on the way.</p>",
}),
});
const result = await response.json();
console.log(result.data.message_id); // "a1b2c3d4-..."

If you prefer SMTP or your framework expects it, use Nodemailer.

Terminal window
npm install nodemailer
const nodemailer = require("nodemailer");
const transporter = nodemailer.createTransport({
host: "smtp.relaypost.dev",
port: 587,
secure: false,
auth: {
user: process.env.SMTP_USERNAME,
pass: process.env.SMTP_PASSWORD,
},
});
await transporter.sendMail({
from: '"Your App" <[email protected]>',
subject: "Your order has shipped",
html: "<h1>Order Shipped</h1><p>Your package is on the way.</p>",
});

Always handle errors when sending email. Common failures include invalid API keys, rate limiting, and validation errors.

try {
const response = await fetch("https://api.relaypost.dev/v1/emails/send", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.RELAYPOST_API_KEY}`,
},
body: JSON.stringify({
from: { email: "[email protected]" },
to: [{ email: "[email protected]" }],
subject: "Hello",
html: "<p>Hello from RelayPost</p>",
}),
});
if (!response.ok) {
const error = await response.json();
console.error("Send failed:", error.error.code, error.error.message);
}
} catch (err) {
console.error("Network error:", err.message);
}

What is the easiest way to send email with Node.js?

Section titled “What is the easiest way to send email with Node.js?”

The easiest way is using the built-in fetch API (Node.js 18+) with a REST email API. No dependencies required — just a single HTTP POST request with your API key and email payload as JSON.

Use the REST API for new projects — it is simpler, faster, and gives you access to advanced features like templates and scheduling. Use SMTP with Nodemailer if your existing codebase already uses SMTP or if you want to switch providers without code changes.

Include an html field in your email payload with the HTML content. Both the REST API and Nodemailer support HTML email. For plain text fallback, also include a text field.