Skip to content

railsware/mailtrap-nodejs

Repository files navigation

Mailtrap Node.js client - Official

TypeScript test NPM downloads

Prerequisites

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

Supported functionality

This NPM package offers integration with the official API for Mailtrap.

Quickly integrate Mailtrap with your Node.js app.

Currently, with this SDK you can:

  • Email API/SMTP
    • Send an email (Transactional and Bulk streams)
    • Send an email with a template
    • Send a batch of emails (Transactional and Bulk streams)
  • Email Sandbox (Testing)
    • Send an email
    • Send an email with a template
    • Message management
    • Inbox management
    • Project management
  • Contact management
    • Contacts CRUD
    • Lists CRUD
  • General
    • Templates CRUD
    • Suppressions management (find and delete)
    • Account access management
    • Permissions management
    • List accounts you have access to

Installation

Use yarn or npm to install the package:

yarn add mailtrap

# or, if you are using NPM:
npm install mailtrap

Usage

Minimal

import { MailtrapClient } from "mailtrap"

/**
 * For this example to work, you need to set up a sending domain,
 * and obtain a token that is authorized to send from the domain.
 */

const TOKEN = "<YOUR-TOKEN-HERE>";
const SENDER_EMAIL = "<[email protected]>";
const RECIPIENT_EMAIL = "<[email protected]>";

const client = new MailtrapClient({ token: TOKEN });

const sender = { name: "Mailtrap Test", email: SENDER_EMAIL };

client
  .send({
    from: sender,
    to: [{ email: RECIPIENT_EMAIL }],
    subject: "Hello from Mailtrap!",
    text: "Welcome to Mailtrap Sending!",
  })
  .then(console.log)
  .catch(console.error);

Nodemailer Transport

NOTE: Nodemailer is needed as a dependency.

yarn add nodemailer

# or, if you are using NPM:
npm install nodemailer

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

yarn add --dev @types/nodemailer

# or, if you are using NPM:
npm install -D @types/nodemailer

You can provide Mailtrap-specific keys like category, customVariables, templateUuid, and templateVariables.

See transport usage below:

Examples

Refer to the examples folder for the source code of this and other advanced examples.

Contacts API

Contact Lists API

Sending API

Batch Sending API

Bulk Sending API

Templates API

Email Sandbox (Testing) API

General API

Development

This library is developed using TypeScript.

Use yarn lint to perform linting with ESLint.

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.