Prompt
how to set up transactional email in node
Latest observation
Oct 21, 2026
OpenAI APIWeb search: off
To set up transactional email in Node.js, the usual approach is to use an email provider like SendGrid, Mailgun, Amazon SES, Postmark, or Resend and send mail through their API or SMTP.
Here’s a simple, practical setup using Node.js + Nodemailer with SMTP.
1) Install Nodemailer
npm install nodemailer
2) Create a transporter
Example with Gmail SMTP:
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS, // use an app password, not your normal password
},
});
For production, it’s better to use a dedicated provider like Postmark, SendGrid, or SES rather than Gmail.
3) Send a transactional email
async function sendWelcomeEmail(to) {
const mailOptions = {
from: process.env.EMAIL_USER,
to,
subject: 'Welcome to our app!',
text: 'Thanks for signing up!',
html: '<h1>Welcome!</h1><p>Thanks for signing up!</p>',
};
try {
const info = await transporter.sendMail(mailOptions);
console.log('Email sent:', info.response);
} catch (err) {
console.error('Error sending email:', err);
}
}
Use it like:
sendWelcomeEmail('user@example.com');
4) Store credentials in environment variables
Create a .env file:
EMAIL_USER=your-email@gmail.com
EMAIL_PASS=your-app-password
Install dotenv:
npm install dotenv
Load it in your app:
require('dotenv').config();
5) Recommended production setup
For reliable transactional email, use a provider such as:
- Postmark — great for transactional email
- SendGrid
- Amazon SES
- Mailgun
- Resend
These give you:
- better deliverability
- bounce handling
- logs/analytics
- templates
- easier scaling
Example with SendGrid
Install
npm install @sendgrid/mail
Setup
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
async function sendWelcomeEmail(to) {
const msg = {
to,
from: 'verified-sender@yourdomain.com',
subject: 'Welcome!',
text: 'Thanks for signing up!',
html: '<strong>Thanks for signing up!</strong>',
};
try {
await sgMail.send(msg);
console.log('Email sent');
} catch (error) {
console.error(error);
}
}
Best practices for transactional email
- Use a verified domain and sender address
- Send through a transactional provider
- Use templates for consistency
- Keep emails short and clear
- Include only important content
- Handle retries and failures
- Avoid sending transactional and marketing emails from the same system if possible
If you want, I can give you:
- a complete Express.js example, or
- a setup using Resend / SendGrid / Amazon SES specifically.