Almost every web app needs to send email: password resets, welcome messages, order confirmations, OTPs. Nodemailer is the standard Node.js library for sending mail, and it works with almost any email service — Gmail, SendGrid, Amazon SES, or your own SMTP server.
Here is a complete example with Gmail:
import nodemailer from 'nodemailer';
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'you@example.com',
pass: 'your-app-password',
},
});
const info = await transporter.sendMail({
from: '"My App" <you@example.com>',
to: 'user@example.com',
subject: 'Welcome to My App',
html: '<h1>Welcome!</h1><p>Your account is ready.</p>',
});
console.log('Message sent:', info.messageId);
For Gmail you need an app password, not your normal password — create one in your Google account security settings. For production, a dedicated service like SendGrid or SES is better, because Gmail limits how many emails you can send per day.
Nodemailer also handles attachments, so you can send invoices as PDFs or images inline in the email:
await transporter.sendMail({
from: '"My App" <you@example.com>',
to: 'user@example.com',
subject: 'Your invoice',
text: 'Your invoice is attached.',
attachments: [{ filename: 'invoice.pdf', path: './invoice.pdf' }],
});
One thing to watch: sending email from your server is only half the job. If your emails land in spam, check that your domain has SPF, DKIM, and DMARC records set up — these tell receiving servers that your mail is legitimate. And always send from a real address that can receive replies.
Nodemailer is for Node.js developers who need to send transactional email — signups, resets, receipts, and notifications. It is simple to start, works with any SMTP service, and scales from a side project to production without changing libraries.
Top comments (0)