How to Send Emails in Node.js (2026 Guide)
Learn how to send transactional emails from Node.js using REST APIs and SMTP. Complete guide with code examples for Express, Next.js, and more.
Why Send Emails from Node.js?
Almost every web application needs to send emails — password resets, order confirmations, notifications, and more. Node.js is one of the most popular server-side runtimes, and sending emails from it should be simple.
There are two main approaches:
- REST API — Make HTTP requests to an email service (recommended)
- SMTP — Use a library like Nodemailer to connect via SMTP
Method 1: REST API (Recommended)
The simplest way to send emails from Node.js is using a REST API. No SMTP configuration, no connection pooling — just a single HTTP request.
// Using fetch (built into Node.js 18+)
const response = await fetch('https://api.unosend.co/v1/emails', {
method: 'POST',
headers: {
'Authorization': 'Bearer un_your_api_key',
'Content-Type': 'application/json',
},
body: JSON.stringify({
from: 'hello@yourdomain.com',
to: 'user@example.com',
subject: 'Welcome to our app!',
html: '<h1>Welcome!</h1><p>Thanks for signing up.</p>',
}),
});
const data = await response.json();
console.log('Email sent:', data.id);With the Unosend SDK
For a better developer experience, use the official SDK:
npm install @unosend/nodeimport { Unosend } from '@unosend/node';
const unosend = new Unosend('un_your_api_key');
const { data, error } = await unosend.emails.send({
from: 'hello@yourdomain.com',
to: 'user@example.com',
subject: 'Welcome!',
html: '<h1>Welcome!</h1>',
});Method 2: SMTP with Nodemailer
If you prefer SMTP, you can use Nodemailer with any SMTP provider:
import nodemailer from 'nodemailer';
const transporter = nodemailer.createTransport({
host: 'smtp.unosend.co',
port: 587,
secure: false,
auth: {
user: 'apikey',
pass: 'un_your_api_key',
},
});
await transporter.sendMail({
from: 'hello@yourdomain.com',
to: 'user@example.com',
subject: 'Hello from Node.js',
html: '<p>Sent via SMTP</p>',
});Express.js Example
import express from 'express';
import { Unosend } from '@unosend/node';
const app = express();
const unosend = new Unosend(process.env.UNOSEND_API_KEY);
app.post('/api/contact', async (req, res) => {
const { name, email, message } = req.body;
const { error } = await unosend.emails.send({
from: 'contact@yourdomain.com',
to: 'team@yourdomain.com',
subject: `Contact form: ${name}`,
html: `<p>From: ${name} (${email})</p><p>${message}</p>`,
});
if (error) return res.status(500).json({ error });
res.json({ success: true });
});Next.js Server Action
'use server'
import { Unosend } from '@unosend/node';
const unosend = new Unosend(process.env.UNOSEND_API_KEY!);
export async function sendWelcomeEmail(email: string, name: string) {
const { error } = await unosend.emails.send({
from: 'welcome@yourdomain.com',
to: email,
subject: `Welcome, ${name}!`,
html: `<h1>Welcome to our app, ${name}!</h1>`,
});
if (error) throw new Error('Failed to send email');
return { success: true };
}Production Best Practices
- Never hardcode API keys — Use environment variables
- Handle errors gracefully — Wrap send calls in try/catch
- Use webhooks — Track delivery, opens, clicks, and bounces
- Verify your domain — Set up SPF, DKIM, and DMARC for better deliverability
- Rate limit your sends — Respect API rate limits and implement retry logic
Conclusion
Sending emails from Node.js takes just a few lines of code with a REST API. Start with 5,000 free emails/month on Unosend and scale from there.
Ready to send your first email?
Get started with 5,000 free emails/month. No credit card required.
Start for free