Sending Email in TypeScript: Libraries and Code Examples

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

Sending Email in TypeScript: Overview of Libraries and Tools



Sending emails is one of the key tasks for web applications. In TypeScript (and its runtime Node.js), there are several mature solutions. In this article, we will cover the most popular libraries, their configuration, and usage examples with full typing.



1. Nodemailer — Universal SMTP Client



Nodemailer is the most popular library for sending email in Node.js. It supports SMTP, built-in transports (Sendmail, SES), and is fully compatible with TypeScript thanks to built-in types.



Installation

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


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, }, });

await transporter.sendMail({ from: `"MyApp" <${process.env.EMAIL_USER}>`, to: options.to, subject: options.subject, text: options.text, html: options.html || options.text, });}

// UsagesendEmail({ to: 'user@example.com', subject: 'Hello from TypeScript!', text: 'This is a test email.',}).catch(console.error);


Working with Attachments

await transporter.sendMail({  to: 'user@example.com',  subject: 'File Attached',  text: 'Please see the attached document.',  attachments: [    {      filename: 'report.pdf',      path: './files/report.pdf',    },  ],});


2. SendGrid — Cloud Email Service



SendGrid (now part of Twilio) provides a powerful API for sending transactional and marketing emails. For TypeScript, there is an official package with ready-made types.



Installation

npm install @sendgrid/mail


Example of sending with typing

import sgMail from '@sendgrid/mail';

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

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

async function sendViaSendGrid(msg: SendGridMessage): Promise<void> { try { await sgMail.send({ to: msg.to, from: msg.from, subject: msg.subject, text: msg.text, html: msg.html, }); console.log('Email sent via SendGrid'); } catch (error) { console.error('SendGrid error:', error); }}

// UsagesendViaSendGrid({ to: 'client@example.com', from: 'noreply@myapp.com', subject: 'Registration Confirmation', text: 'Welcome!', html: '<h1>Welcome!</h1><p>Thank you for registering</p>',});


3. AWS SES — For High-Load Systems



AWS Simple Email Service (SES) is an enterprise solution for sending large volumes of emails. It is used via the @aws-sdk/client-ses SDK.



Installation

npm install @aws-sdk/client-ses


Example with TypeScript

import { SESClient, SendEmailCommand } from '@aws-sdk/client-ses';

const sesClient = new SESClient({ region: 'us-east-1', credentials: { accessKeyId: process.env.AWS_ACCESS_KEY!, secretAccessKey: process.env.AWS_SECRET_KEY!, },});

interface SESEmailParams { to: string[]; from: string; subject: string; bodyText: string; bodyHtml?: string;}

async function sendViaSES(params: SESEmailParams): Promise<void> { const command = new SendEmailCommand({ Destination: { ToAddresses: params.to, },

Recommendations