Email Sending in TypeScript: A Complete Guide with nodemailer and SMTP

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: From Simple to Complex



Sending emails is one of the key functions of any web application: user registration, password reset, order notifications, newsletters. In the TypeScript (and Node.js) ecosystem, there are several powerful libraries that allow you to flexibly configure email sending. In this article, we will cover three popular tools: nodemailer, @sendgrid/mail, and mailgun.js, show code examples in TypeScript, discuss secure work with SMTP and templating.



1. nodemailer — Universal SMTP Client



nodemailer is the most popular library for sending emails from Node.js. It supports any SMTP servers (Gmail, Yandex, Mail.ru, your own mail server), as well as built-in transporters for popular services. It works "out of the box" with TypeScript thanks to the availability of types.



Installation

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


Basic Example of Sending via Gmail SMTP

import nodemailer from 'nodemailer';

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

async function sendEmail(options: EmailOptions): Promise<void> { const transporter = nodemailer.createTransport({ service: 'gmail', auth: { user: process.env.EMAIL_USER, pass: process.env.EMAIL_PASS, }, });

const mailOptions = { from: `"My App" <${process.env.EMAIL_USER}>`, to: options.to, subject: options.subject, text: options.text, html: options.html, };

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

// UsagesendEmail({ to: 'user@example.com', subject: 'Hello from TypeScript!', text: 'This is a test email.', html: '<h1>Hello!</h1><p>This is the <strong>HTML</strong> version.</p>',}).catch(console.error);


Working with Multiple Recipients and Attachments

async function sendBulkWithAttachments(): Promise<void> {  const transporter = nodemailer.createTransport({    host: 'smtp.yandex.ru',    port: 465,    secure: true,    auth: {      user: process.env.YANDEX_USER,      pass: process.env.YANDEX_PASS,    },  });

await transporter.sendMail({ from: '"Team" <no-reply@mydomain.ru>', to: ['user1@example.com', 'user2@example.com'], subject: 'Monthly Report', text: 'The report file is attached.', attachments: [ { filename: 'report.pdf', path: './docs/report.pdf', }, ], });}


2. SendGrid — Cloud Service for Large Volumes



SendGrid (part of Twilio) is a professional platform for email campaigns. It provides an API, SDK for Node.js/TypeScript, delivery analytics, and spam protection. Recommended for high-load projects (thousands of emails per day).



Installation and Setup

npm install @sendgrid/mail


import sgMail from '@sendgrid/mail';

sgMail.setApiKey(process.env.SENDGRID_API_KEY!);

interface SendGridOptions { to: string | string[]; subject: string; html: string; from?: string;}

async function sendViaSendGrid(options: SendGridOptions): Promise<void> { const msg = { to: options.to, from: options.from || 'noreply@mydomain.com', subject: options.subject, html: options.html, };

try { await sgMail.send(msg); console.log('Email sent via SendGrid'); } catch (error) { console.error('SendGrid error:', error); throw error; }}


3. Mailgun — An Alternative for Developers



Mailgun is another popular service with a REST API and TypeScript compatibility

Recommendations