Send Email with Node.js
How to Send Email with Node.js
Section titled “How to Send Email with Node.js”- Sign up for an email delivery service and generate an API key
- Verify your sending domain with SPF and DKIM records
- Choose your sending method — REST API (fetch) or SMTP (Nodemailer)
- Build the email payload with sender, recipient, subject, and HTML body
- Send the email and handle the response
- Set up webhooks to track delivery, bounces, and opens
Method 1: REST API with fetch
Section titled “Method 1: REST API with fetch”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({ 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-..."Method 2: SMTP with Nodemailer
Section titled “Method 2: SMTP with Nodemailer”If you prefer SMTP or your framework expects it, use Nodemailer.
npm install nodemailerconst 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({ subject: "Your order has shipped", html: "<h1>Order Shipped</h1><p>Your package is on the way.</p>",});Error Handling
Section titled “Error Handling”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({ 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);}Frequently Asked Questions
Section titled “Frequently Asked Questions”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.
Should I use SMTP or API for Node.js?
Section titled “Should I use SMTP or API for Node.js?”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.
How do I send HTML email with Node.js?
Section titled “How do I send HTML email with Node.js?”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.