@nxtwebmasters/nxt-mailer v1.1.1
NXTWEBMASTERS/NXT MAILER
A lightweight, zero-dependency Node.js module for sending transactional emails using Gmail (or any SMTP) via Nodemailer. Perfect for serverless functions, microservices, or any Node.js app needing quick email capabilities.
π¦ Installation
Install viaΒ NPM:
npm install @nxtwebmasters/nxt-mailerInstall viaΒ Yarn:
yarn add @nxtwebmasters/nxt-mailerπ§ Quick Start
- Import Package
import { createTransporter, sendEmail } from "@nxtwebmasters/nxt-mailer";- Create Transporter with your SMTP credentials
(async () => {
const transporter = createTransporter({
host: "smtp.gmail.com",
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASSWORD,
},
});- Send Email
await sendEmail(
{
from: '"Acme Inc." <no-reply@acme.com>',
to: ["alice@example.com", "bob@example.com"],
cc: ["manager@example.com"],
bcc: ["audit@example.com"],
subject: "Welcome to Acme!",
text: "Hello there!",
html: "<p>Hello <strong>there</strong>!</p>",
attachments: [{ filename: "terms.pdf", path: "./docs/terms.pdf" }],
},
transporter
);
console.log("β
Email sent successfully");
})();π Table of Contents
- Features
- API Reference
- Configuration Options
- Usage Examples
- Error Handling
- Testing
- Contributing
- Change Log
- License
β Features
- Zero dependencies beyond Nodemailer
- Gmail SMTP presets (but works with any SMTP)
- Multi-recipient support: To, CC, BCC
- Rich content: plain-text, HTML, inline images
- Attachments: files, streams, buffers
- Promise-based API with async/await
- TypeScript type definitions included
π API Reference
createTransporter(options)
Creates and returns a Nodemailer transporter instance.
| Property | Type | Required | Default | Description |
|---|---|---|---|---|
host | string | β | β | SMTP server hostname (e.g., "smtp.gmail.com") |
port | number | β | β | SMTP port (465 for SSL, 587 for TLS) |
secure | boolean | β | false | true for port 465 (SSL), false for others |
auth | object | β | β | Authentication object |
ββ user | string | β | β | SMTP username (e.g., your email address) |
ββ pass | string | β | β | SMTP password or app-specific password |
logger | boolean | β | false | Enable Nodemailer logging |
debug | boolean | β | false | Show SMTP traffic for debugging |
interface TransportOptions {
host: string;
port: number;
secure: boolean;
auth: {
user: string;
pass: string;
};
logger?: boolean;
debug?: boolean;
}sendEmail(mailOptions, transporter)
Sends an email using the provided transporter. Returns a promise that resolves with the Nodemailer info object.
| Property | Type | Required | Description |
|---|---|---|---|
from | string | β | Sender address (e.g., "Name <email@domain.com>") |
to | string | string[] | β | Primary recipient(s) |
cc | string | string[] | β | CC recipient(s) |
bcc | string | string[] | β | BCC recipient(s) |
subject | string | β | Email subject line |
text | string | β | Plain-text body |
html | string | β | HTML body |
attachments | Attachment[] | β | Array of attachment objects (see below) |
interface MailOptions {
from: string;
to: string | string[];
cc?: string | string[];
bcc?: string | string[];
subject: string;
text?: string;
html?: string;
attachments?: Attachment[];
}
interface Attachment {
filename?: string;
path?: string;
href?: string;
content?: Buffer | string;
contentType?: string;
cid?: string; // inline images
}Returns:Promise<import("nodemailer").SentMessageInfo>
βοΈ Configuration Options
Beyond SMTP auth, you can pass any Nodemailer transporter options:
const transporter = createTransporter({
host: process.env.SMTP_HOST,
port: +process.env.SMTP_PORT,
secure: process.env.SMTP_SECURE === "true",
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,
},
logger: true, // Turn on Nodemailer logger
debug: true, // Show SMTP traffic
});π‘ Usage Examples
1. Sending Inline Images
await sendEmail(
{
from: "no-reply@acme.com",
to: ["user@example.com"],
subject: "Inline Images Example",
html: '<h1>Logo</h1><img src="cid:logo@acme"/>',
attachments: [
{ filename: "logo.png", path: "./assets/logo.png", cid: "logo@acme" },
],
},
transporter
);2. Sending Buffers or Streams
import fs from "fs";
const pdfBuffer = fs.readFileSync("./reports/summary.pdf");
await sendEmail(
{
from: "reports@acme.com",
to: "manager@acme.com",
subject: "Monthly Report",
text: "Please find the report attached.",
attachments: [{ filename: "report.pdf", content: pdfBuffer }],
},
transporter
);β οΈ Error Handling
sendEmail will throw if sending fails. Wrap calls in try/catch:
try {
await sendEmail(
{
/* ... */
},
transporter
);
console.log("β
Success");
} catch (err) {
console.error("β Email failed:", err);
}Common errors include authentication failures, network timeouts, or invalid addresses.
π§ͺ Testing
Tests are written with Jest. To run:
npm testTest Coverage
npm run test:coverageπ€ Contributing
We welcome contributions! Please:
- Fork the repo
- Create a feature branch (
git checkout -b feat/my-feature) - Commit your changes (
git commit -m 'feat: add new feature') - Push to your branch (
git push origin feat/my-feature) - Open a Pull Request
See CONTRIBUTING.md for details.
π Change Log
See CHANGELOG.md for version history and release notes.
π License
This project is licensed under the MIT License. See the LICENSE file for details.
Β© 2025 NXT Webmasters. All rights reserved.