MailerSend Node.js SDK

2.8.0 · active · verified Tue Apr 21

The MailerSend Node.js SDK provides a comprehensive programmatic interface for interacting with the MailerSend API, enabling developers to efficiently send transactional and marketing emails, manage SMS campaigns, configure domains, set up webhooks, utilize templates, and track analytics data. The current stable version is `2.8.0`, released with a consistent and active development cadence, typically seeing updates monthly or bi-monthly. This commitment ensures the library remains up-to-date with API changes and introduces new features regularly. Key differentiators include its focus on high email deliverability, an intuitive API design that simplifies complex email operations, and built-in integrations for streamlined workflows. The SDK fully supports both email and SMS functionalities and ships with comprehensive TypeScript types, enhancing developer experience through improved type safety and autocompletion.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the MailerSend client and send a basic HTML email using environment variables for configuration. It covers setting up sender and recipient details and handling potential errors.

import { MailerSend, EmailParams, Sender, Recipient } from 'mailersend';
import * as dotenv from 'dotenv';

dotenv.config();

const MAIL_FROM_ADDRESS = process.env.MAIL_FROM_ADDRESS || 'info@yourdomain.com';
const MAIL_FROM_NAME = process.env.MAIL_FROM_NAME || 'Your Company Name';
const MAIL_TO_ADDRESS = process.env.MAIL_TO_ADDRESS || 'recipient@example.com';
const MAIL_TO_NAME = process.env.MAIL_TO_NAME || 'Recipient Name';

const mailerSend = new MailerSend({
  apiKey: process.env.MAILERSEND_API_TOKEN || '',
});

if (!process.env.MAILERSEND_API_TOKEN) {
  console.error('Error: MAILERSEND_API_TOKEN environment variable is not set.');
  process.exit(1);
}

async function sendTestEmail() {
  const sender = new Sender(MAIL_FROM_ADDRESS, MAIL_FROM_NAME);
  const recipients = [new Recipient(MAIL_TO_ADDRESS, MAIL_TO_NAME)];

  const emailParams = new EmailParams()
    .setFrom(sender)
    .setTo(recipients)
    .setReplyTo(sender)
    .setSubject('MailerSend Node.js SDK Test Email')
    .setHtml('<strong>This is an automated test email from MailerSend Node.js SDK!</strong>')
    .setText('This is an automated test email from MailerSend Node.js SDK!');

  try {
    const response = await mailerSend.email.send(emailParams);
    console.log('Email sent successfully:', response.body);
  } catch (error: any) {
    console.error('Error sending email:', error.response?.body || error.message);
  }
}

sendTestEmail();

view raw JSON →