npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

mailtrap

v4.4.0

Published

Official mailtrap.io API client

Readme

Mailtrap Node.js client - Official

TypeScript test NPM downloads

Prerequisites

To get the most out of this official Mailtrap.io Node.js SDK:

Installation

You can install the package via npm or yarn:

npm install mailtrap

# or, if you are using Yarn:
yarn add mailtrap

Usage

You should use ES modules or CommonJS imports in your application to load the package.

Minimal usage (Transactional sending)

The quickest way to send a single transactional email with only the required parameters:

import { MailtrapClient } from "mailtrap";

const mailtrap = new MailtrapClient({
  token: process.env.MAILTRAP_API_KEY, // your API key here https://mailtrap.io/api-tokens
});

mailtrap
  .send({
    from: { name: "Mailtrap Test", email: "[email protected]" },
    to: [{ email: "[email protected]" }],
    subject: "Hello from Mailtrap Node.js",
    text: "Plain text body",
  })
  .then(console.log)
  .catch(console.error);

Sandbox vs Production (easy switching)

Mailtrap lets you test safely in the Email Sandbox and then switch to Production (Sending) with one flag.

Example .env variables (or export in shell):

MAILTRAP_API_KEY=your_api_token # https://mailtrap.io/api-tokens
MAILTRAP_USE_SANDBOX=true       # true/false toggle
MAILTRAP_INBOX_ID=123456        # Only needed for sandbox

Bootstrap logic:

import { MailtrapClient } from "mailtrap";

const apiKey = process.env.MAILTRAP_API_KEY;
const isSandbox = process.env.MAILTRAP_USE_SANDBOX === "true";
const inboxId = isSandbox ? Number(process.env.MAILTRAP_INBOX_ID) : undefined; // required only for sandbox

const client = new MailtrapClient({
  token: apiKey,
  sandbox: isSandbox,
  testInboxId: inboxId, // undefined is ignored for production
});

client
  .send({
    from: {
      name: "Mailtrap Test",
      email: isSandbox ? "[email protected]" : "[email protected]",
    },
    to: [{ email: "[email protected]" }],
    subject: isSandbox ? "[SANDBOX] Demo email" : "Welcome onboard",
    text: "This is a minimal body for demonstration purposes.",
  })
  .then(console.log)
  .catch(console.error);

Bulk stream example (optional) differs only by setting bulk: true:

const bulkClient = new MailtrapClient({ token: apiKey, bulk: true });

Recommendations:

  • Toggle sandbox with MAILTRAP_USE_SANDBOX.

  • Use separate API tokens for Production and Sandbox.

  • Keep initialisation in a single factory object/service so that switching is centralised.

Full-featured usage example

import { MailtrapClient } from "mailtrap";
import fs from "node:fs";
import path from "node:path";

// Init Mailtrap client depending on your needs
const mailtrap = new MailtrapClient({
  token: process.env.MAILTRAP_API_KEY, // your API token
  bulk: false, // set to true for bulk email sending (false by default)
  sandbox: false, // set to true for sandbox mode (false by default)
  testInboxId: undefined, // optional, only for sandbox mode
});

const welcomeImage = fs.readFileSync(path.join(__dirname, "welcome.png"));

mailtrap
  .send({
    category: "Integration Test",
    custom_variables: {
      user_id: "45982",
      batch_id: "PSJ-12",
    },
    from: { name: "Mailtrap Test", email: "[email protected]" },
    reply_to: { email: "[email protected]" },
    to: [{ name: "Jon", email: "[email protected]" }],
    cc: [{ email: "[email protected]" }, { email: "[email protected]" }],
    bcc: [{ email: "[email protected]" }],
    subject: "Best practices of building HTML emails",
    text: "Hey! Learn the best practices of building HTML emails and play with ready-to-go templates. Mailtrap's Guide on How to Build HTML Email is live on our blog",
    html: `
      <html>
        <body>
          <p><br>Hey</br>
          Learn the best practices of building HTML emails and play with ready-to-go templates.</p>
          <p><a href="https://mailtrap.io/blog/build-html-email/">Mailtrap's Guide on How to Build HTML Email</a> is live on our blog</p>
          <img src="cid:welcome.png">
        </body>
      </html>
    `,
    attachments: [
      {
        filename: "welcome.png",
        content_id: "welcome.png",
        disposition: "inline",
        content: welcomeImage,
      },
    ],
    headers: {
      "X-Message-Source": "domain.com",
      "X-Mailer": "Mailtrap Node.js Client",
    },
  })
  .then(console.log)
  .catch(console.error);

// OR Template email sending
mailtrap
  .send({
    from: { name: "Mailtrap Test", email: "[email protected]" },
    reply_to: { email: "[email protected]" },
    to: [{ name: "Jon", email: "[email protected]" }],
    template_uuid: "bfa432fd-0000-0000-0000-8493da283a69",
    template_variables: {
      user_name: "Jon Bush",
      next_step_link: "https://mailtrap.io/",
      get_started_link: "https://mailtrap.io/",
      onboarding_video_link: "some_video_link",
      company: {
        name: "Best Company",
        address: "Its Address",
      },
      products: [
        {
          name: "Product 1",
          price: 100,
        },
        {
          name: "Product 2",
          price: 200,
        },
      ],
      isBool: true,
      int: 123,
    },
  })
  .then(console.log)
  .catch(console.error);

Nodemailer Transport

NOTE: Nodemailer is needed as a dependency.

npm install nodemailer

# or, if you are using Yarn:
yarn add nodemailer

If you're using TypeScript, install @types/nodemailer as a devDependency:

npm install -D @types/nodemailer

# or, if you are using Yarn:
yarn add --dev @types/nodemailer

You can provide Mailtrap-specific keys like category, custom_variables, template_uuid, and template_variables.

See transport usage below:

Supported functionality & Examples

Email API:

Email Sandbox (Testing):

Contact management:

General API:

Contributing

Bug reports and pull requests are welcome on GitHub. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The package is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Mailtrap project's codebases, issue trackers, chat rooms, and mailing lists is expected to follow the code of conduct.

Compatibility with previous releases

Versions of this package up to 2.0.2 were an unofficial client developed by @vchin. Package version 3 is a completely new package.