Nodemailer

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Nodemailer: Sending email in TypeScript

Nodemailer is the most popular module for sending emails from Node.js and TypeScript applications. It allows you to easily integrate email sending via SMTP, and also supports HTML templates, attachments, streams, and many transports (Gmail, Yandex, SendGrid, etc.). The library is fully compatible with TypeScript thanks to built-in types.

Why do you need Nodemailer?

In modern web development, sending email is required for:

  • Registration confirmation (verification)
  • Password recovery
  • Notifications and newsletters
  • System alerts

Nodemailer simplifies this process by providing a unified API for any email service.

Installation

npm install nodemailernpm install -D @types/nodemailer

For TypeScript, be sure to install the types package.

Main features

  • SMTP transport — sending via any SMTP server (Gmail, Mail.ru, Yandex, AWS SES).
  • HTML and text — support for both plain text and HTML emails.
  • Attachments — files, images, Buffer, streams.
  • Templating — integration with handlebars, pug, and others.
  • Streams — sending large files without loading into memory.
  • Connection pool — improved performance for mass mailings.

Code example in TypeScript

import nodemailer from 'nodemailer';

interface MailOptions { to: string; subject: string; text?: string; html?: string;}

async function sendEmail(options: MailOptions): Promise<void> { // Create transport (example for Gmail) const transporter = nodemailer.createTransport({ service: 'gmail', auth: { user: 'your-email@gmail.com', pass: 'your-app-password' } });

// Email settings const mailData = { from: 'your-email@gmail.com', to: options.to, subject: options.subject, text: options.text || '', html: options.html || undefined };

// Sending const info = await transporter.sendMail(mailData); console.log('Email sent:', info.messageId);}

// UsagesendEmail({ to: 'user@example.com', subject: 'Hello from TypeScript!', html: '<h1>Welcome</h1><p>This is a test email.</p>'});

When to use Nodemailer

The library is suitable for:

  • Any Node.js/TypeScript projects that need email sending.
  • Prototypes and MVPs — quick setup via Gmail.
  • Production systems — using transactional services (SendGrid, Mailgun).
  • Microservices and serverless (Lambda, Cloud Functions).

It is not recommended to use Nodemailer for mass spam mailings — specialized ESPs (Email Service Providers) exist for that.

Conclusion

Nodemailer is a mature, stable, and well-documented library. Thanks to TypeScript types, you get autocompletion and error checking at compile time. It is an ideal choice for sending email in the JavaScript/TypeScript ecosystem.

Recommendations