Official Mailtrap Node.js client
This NPM package offers integration with the official API for Mailtrap.
Quickly add email sending functionality to your Node.js application with Mailtrap.
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. It is still under development and does not support the testing API yet. Please continue using version 2 if you need access to the testing API.
Installation
Use yarn or npm to install the package:
yarn add mailtrap
npm install mailtrap
Usage
Refer to the examples
folder for the source code of these examples.
Minimal
import { MailtrapClient } from "mailtrap"
const TOKEN = "<YOUR-TOKEN-HERE>";
const SENDER_EMAIL = "<SENDER@YOURDOMAIN.COM>";
const RECIPIENT_EMAIL = "<RECIPIENT@EMAIL.COM>";
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);
Full
import fs from "node:fs"
import path from "node:path"
import { MailtrapClient } from "mailtrap"
const TOKEN = "<YOUR-TOKEN-HERE>";
const SENDER_EMAIL = "<SENDER@YOURDOMAIN.COM>";
const RECIPIENT_EMAIL = "<RECIPIENT@EMAIL.COM>";
const client = new MailtrapClient({ token: TOKEN });
const sender = { name: "Mailtrap Test", email: SENDER_EMAIL };
const welcomeImage = fs.readFileSync(path.join(__dirname, "welcome.png"));
client
.send({
category: "test",
custom_variables: {
hello: "world",
year: 2022,
anticipated: true,
},
from: sender,
to: [{ email: RECIPIENT_EMAIL }],
subject: "Hello from Mailtrap!",
html: `
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body style="font-family: sans-serif;">
<div style="display: block; margin: auto; max-width: 600px;" class="main">
<h1 style="font-size: 18px; font-weight: bold; margin-top: 20px">Congrats for sending test email with Mailtrap!</h1>
<p>Inspect it using the tabs you see above and learn how this email can be improved.</p>
<img alt="Inspect with Tabs" src="cid:welcome.png" style="width: 100%;">
<p>Now send your email using our fake SMTP server and integration of your choice!</p>
<p>Good luck! Hope it works.</p>
</div>
<!-- Example of invalid for email html/css, will be detected by Mailtrap: -->
<style>
.main { background-color: white; }
a:hover { border-left-width: 1em; min-height: 2em; }
</style>
</body>
</html>
`,
attachments: [
{
filename: "welcome.png",
content_id: "welcome.png",
disposition: "inline",
content: welcomeImage,
},
],
})
.then(console.log)
.catch(console.error)
Mail from template
import { MailtrapClient } from "mailtrap"
const TOKEN = "<YOUR-TOKEN-HERE>";
const SENDER_EMAIL = "<SENDER@YOURDOMAIN.COM>";
const RECIPIENT_EMAIL = "<RECIPIENT@EMAIL.COM>";
const client = new MailtrapClient({ token: TOKEN });
client
.send({
from: { name: "Mailtrap Test", email: SENDER_EMAIL },
to: [{ email: RECIPIENT_EMAIL }],
template_uuid: "813e39db-c74a-4830-b037-0e6ba8b1fe88",
template_variables: {
user_name: "John Doe",
},
})
.then(console.log)
.catch(console.error);
Nodemailer Transport
NOTE: Nodemailer is needed as a dependency.
yarn add nodemailer
npm install --s nodemailer
If you're using Typescript, install @types/nodemailer
as a devDependency
.
yarn add --dev @types/nodemailer
npm install --s-dev @types/nodemailer
You can provide Mailtrap specific keys like category
, customVariables
, templateUuid
and templateVariables
.
import { readFileSync } from "fs";
import Nodemailer from "nodemailer";
import { MailtrapTransport } from "mailtrap"
const TOKEN = "<YOUR-TOKEN-HERE>"
const SENDER_EMAIL = "<SENDER@YOURDOMAIN.COM>";
const RECIPIENT_EMAIL = "<RECIPIENT@EMAIL.COM>";
const transport = Nodemailer.createTransport(MailtrapTransport({
token: TOKEN
}))
transport.sendMail({
text: "Welcome to Mailtrap Sending!",
to: {
address: RECIPIENT_EMAIL,
name: "John Doe"
},
from: {
address: SENDER_EMAIL,
name: "Mailtrap Test"
},
subject: "Hello from Mailtrap!",
html: `
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body style="font-family: sans-serif;">
<div style="display: block; margin: auto; max-width: 600px;" class="main">
<h1 style="font-size: 18px; font-weight: bold; margin-top: 20px">Congrats for sending test email with Mailtrap!</h1>
<p>Inspect it using the tabs you see above and learn how this email can be improved.</p>
<img alt="Inspect with Tabs" src="cid:welcome.png" style="width: 100%;">
<p>Now send your email using our fake SMTP server and integration of your choice!</p>
<p>Good luck! Hope it works.</p>
</div>
<!-- Example of invalid for email html/css, will be detected by Mailtrap: -->
<style>
.main { background-color: white; }
a:hover { border-left-width: 1em; min-height: 2em; }
</style>
</body>
</html>
`,
attachments: [
{
filename: "welcome.png",
content: readFileSync("./welcome.png"),
},
],
}).then(console.log)
.catch(console.error)
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.