Prompt
How do I set up transactional emails in Node?
Latest observation
Oct 12, 2026
OpenAI APIWeb search: off
To set up transactional emails in Node.js, the usual flow is:
- Choose an email provider
- Configure SMTP or provider API credentials
- Build a reusable mailer utility
- Send emails from your app logic
- Handle retries, templates, and errors
A common, simple approach is using Nodemailer with an SMTP provider like SendGrid, Mailgun, Postmark, Amazon SES, or Gmail (not ideal for production).
1) Install Nodemailer
npm install nodemailer
2) Set up environment variables
Put credentials in .env:
SMTP_HOST=smtp.example.com
SMTP_PORT=587
SMTP_USER=your_username
SMTP_PASS=your_password
SMTP_FROM="Your App <no-reply@yourapp.com>"
Load them with dotenv:
npm install dotenv
3) Create a mailer utility
// mailer.js
import nodemailer from 'nodemailer';
import dotenv from 'dotenv';
dotenv.config();
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: Number(process.env.SMTP_PORT),
secure: false, // true for 465, false for 587
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,
},
});
export async function sendEmail({ to, subject, text, html }) {
return transporter.sendMail({
from: process.env.SMTP_FROM,
to,
subject,
text,
html,
});
}
4) Use it in your app
// example.js
import { sendEmail } from './mailer.js';
await sendEmail({
to: 'user@example.com',
subject: 'Welcome!',
text: 'Thanks for signing up.',
html: '<h1>Thanks for signing up</h1><p>We’re glad you’re here.</p>',
});
5) Example: password reset email
await sendEmail({
to: user.email,
subject: 'Reset your password',
html: `
<p>Click below to reset your password:</p>
<a href="${resetLink}">Reset Password</a>
`,
});
6) Recommended production practices
- Use a real email provider instead of raw SMTP from a personal account
- Verify your domain and configure:
- SPF
- DKIM
- DMARC
- Use templates for consistency
- Queue emails for reliability if sending many emails
- Log errors and handle retries
- Never put secrets in code
- Use a no-reply address for transactional mail
7) If you want API-based sending instead of SMTP
Many providers offer an API SDK, which is often easier and more reliable than SMTP. For example, with SendGrid:
npm install @sendgrid/mail
import sgMail from '@sendgrid/mail';
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
await sgMail.send({
to: 'user@example.com',
from: 'no-reply@yourapp.com',
subject: 'Welcome!',
text: 'Thanks for signing up.',
});
8) Best choice by use case
- Small app / quick setup: Nodemailer + SMTP
- Production transactional emails: Postmark, SendGrid, Mailgun, SES
- High reliability / deliverability: Postmark or SES with verified domain
If you want, I can also give you:
- a Node/Express transactional email setup,
- a React/Next.js + Node backend example, or
- a Postmark/SendGrid implementation.