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

Email sending in TypeScript: Nodemailer library

Nodemailer is the most popular module for sending emails from Node.js and TypeScript. It allows sending emails via SMTP, as well as through built-in transport services (SendGrid, Mailgun, and others). The library fully supports TypeScript (including typings) and works with both text and HTML emails, attachments, and encryption.

Why is nodemailer needed?

  • Sending transactional emails (registration confirmation, password recovery).
  • Sending notifications (for example, after an order on a website).
  • Integration with feedback forms.
  • Automation of reports (daily data dumps).

Installation

npm install nodemailernpm install @types/nodemailer --save-dev

Main features

  • SMTP transport — configuration via any SMTP server (Gmail, Yandex, Mail.ru).
  • HTML and text support — you can send both plain text and beautiful HTML templates.
  • Attachments — files, images, PDF.
  • Streaming — sending large files without loading them into memory.
  • Typings — full TypeScript support (interfaces for options).

TypeScript code example

import nodemailer from 'nodemailer';

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

async function sendEmail(options: MailOptions): Promise<void> { const transporter = nodemailer.createTransport({ host: 'smtp.gmail.com', port: 587, secure: false, auth: { user: 'your-email@gmail.com', pass: 'your-app-password' } });

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

// UsagesendEmail({ from: 'noreply@example.com', 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 ideal for server-side applications on Node.js/TypeScript, especially in conjunction with Express, Next.js, or Nest.js. If you need to quickly set up email sending without complex configurations — nodemailer is the best choice. It is also suitable for microservices and background tasks (for example, using Bull or Agenda).

Important notes

  • For Gmail, use an app password (not your main password).
  • For production, use environment variables (e.g., process.env.SMTP_PASS).
  • Don't forget about error handling: try/catch is mandatory.

Nodemailer is the de facto standard for sending email in TypeScript. Lightweight, reliable, and with excellent documentation.

Recommendations