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 library for sending email in TypeScript

nodemailer is a powerful and popular library for Node.js, written in TypeScript, that makes it easy to send emails from server-side applications. It supports all modern protocols (SMTP, SES, Sendmail) and email formats (HTML, text, attachments).

Why is nodemailer needed?

In web development, it is often necessary to send notifications, registration confirmations, password resets, or marketing newsletters. nodemailer solves this problem by providing a simple and flexible API for working with mail servers. Thanks to TypeScript typing, the code becomes more reliable and predictable.

Installation

Install the package via npm or yarn:

npm install nodemailernpm install -D @types/nodemailer

For TypeScript, be sure to install the types package.

Main features

  • SMTP support — works with any SMTP server (Gmail, Yandex, Mailgun, SendGrid).
  • HTML and text emails — you can send both plain text and formatted HTML messages.
  • Attachments — support for files, images, and inline attachments.
  • Streams — ability to send large files via streams.
  • Security — support for TLS/SSL, OAuth2.
  • Connection pool — optimization for mass mailing.

Code example in TypeScript

A simple example of sending an email via Gmail SMTP:

import nodemailer from 'nodemailer';

async function sendEmail() { const transporter = nodemailer.createTransport({ host: 'smtp.gmail.com', port: 587, secure: false, // true for 465 auth: { user: 'your-email@gmail.com', pass: 'your-password', }, });

const info = await transporter.sendMail({ from: '"Sender Name" <your-email@gmail.com>', to: 'recipient@example.com', subject: 'Hello from TypeScript!', text: 'This is the email text.', html: '<b>This is an HTML email</b>', });

console.log('Email sent:', info.messageId);}

sendEmail().catch(console.error);

When to use nodemailer

The library is ideal for:

  • Backend applications on Node.js/Express/NestJS.
  • Automatic notifications (registration, orders, errors).
  • Email address confirmation.
  • Sending transactional emails.
  • Integration with any SMTP providers.

If you need to send emails with attachments, HTML templates, or via external services (SendGrid, Mailgun), nodemailer is the de facto standard in the Node.js ecosystem.

Important: For production, use environment variables to store passwords and configure OAuth2 to avoid exposing credentials in the code.

Recommendations