Nodemailer Send Email with SMTP
Send transactional emails using Nodemailer with SMTP transport, HTML templates, and attachments.
import nodemailer from 'nodemailer';
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: Number(process.env.SMTP_PORT) || 587,
secure: false,
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,
},
});
export async function sendEmail({
to,
subject,
html,
}: {
to: string;
subject: string;
html: string;
}) {
const info = await transporter.sendMail({
from: '"SnippetsLab" <noreply@snippetslab.dev>',
to,
subject,
html,
});
return info.messageId;
}Sponsored
Try Resend — Email API for Developers
Use Cases
- Welcome emails
- Password reset emails
- Order confirmations
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
JWT Verify Middleware
Express middleware that verifies JWT tokens from the Authorization header and attaches the decoded payload to the request.
In-Memory Rate Limiter for Express
Token bucket rate limiter middleware for Express with configurable window and max requests per IP.
Async Error Handler Wrapper
Higher-order function that wraps async Express route handlers and forwards rejected promises to error middleware.
Environment Variable Validator
Validates required environment variables at startup and returns a typed config object or throws with missing keys.