1.1.1 β€’ Published 7 months ago

@nxtwebmasters/nxt-mailer v1.1.1

Weekly downloads
-
License
ISC
Repository
github
Last release
7 months ago

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-mailer

Install 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

  1. Features
  2. API Reference
  3. Configuration Options
  4. Usage Examples
  5. Error Handling
  6. Testing
  7. Contributing
  8. Change Log
  9. 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.

PropertyTypeRequiredDefaultDescription
hoststringβœ“β€”SMTP server hostname (e.g., "smtp.gmail.com")
portnumberβœ“β€”SMTP port (465 for SSL, 587 for TLS)
securebooleanβœ“falsetrue for port 465 (SSL), false for others
authobjectβœ“β€”Authentication object
└─ userstringβœ“β€”SMTP username (e.g., your email address)
└─ passstringβœ“β€”SMTP password or app-specific password
loggerbooleanβœ—falseEnable Nodemailer logging
debugbooleanβœ—falseShow 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.

PropertyTypeRequiredDescription
fromstringβœ“Sender address (e.g., "Name <email@domain.com>")
tostring | string[]βœ“Primary recipient(s)
ccstring | string[]βœ—CC recipient(s)
bccstring | string[]βœ—BCC recipient(s)
subjectstringβœ“Email subject line
textstringβœ—Plain-text body
htmlstringβœ—HTML body
attachmentsAttachment[]βœ—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 test

Test Coverage

npm run test:coverage

🀝 Contributing

We welcome contributions! Please:

  1. Fork the repo
  2. Create a feature branch (git checkout -b feat/my-feature)
  3. Commit your changes (git commit -m 'feat: add new feature')
  4. Push to your branch (git push origin feat/my-feature)
  5. 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.

1.1.1

7 months ago

1.1.0

7 months ago

1.0.0

7 months ago