Skip to main content

Plugins

Nodemailer is designed to be extensible. You can inject custom logic at three well‑defined phases of a message’s lifecycle:

PhaseKeywordWhen it runsTypical uses
Pre‑processingcompileRight after the message object is built, before the MIME source is generatedTemplating, automatic plain‑text alternatives, address validation
ProcessingstreamWhile the MIME stream is being generatedDKIM signing, inlining images, transforming HTML
SendingtransportAfter the message source is ready, to actually deliver itSMTP, SES, SparkPost, custom HTTP APIs
tip

Prefer compile and stream plugins for portability. Transport plugins are only required when you need complete control over delivery.

Writing a plugin

// commonjs — works on Node.js ≥ 6.0.0
module.exports = function myCompilePlugin(mail, callback) {
// `mail` is the Nodemailer Mail object
// Add or adjust properties before the MIME source is generated
if (!mail.data.text && mail.data.html) {
mail.data.text = require("html-to-text").htmlToText(mail.data.html);
}

callback(); // Always invoke the callback (or pass an Error)
};

Register the plugin on a transport instance:

const nodemailer = require("nodemailer");
const transport = nodemailer.createTransport({ sendmail: true });

transport.use("compile", require("./myCompilePlugin"));

Error handling

If your plugin encounters a fatal problem, pass an Error object to the callback:

callback(new Error("Template not found"));

The message will not be sent and the error will propagate to the caller’s sendMail() callback/promise.

Public plugins

A curated selection of community‑maintained plugins:

Looking for something else? Try searching npm for “nodemailer plugin”.


Need more power? See Creating plugins » for a deep dive into the plugin API.