npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

better-mail

v1.0.2

Published

A world-class Node.js email sending library with support for multiple providers and template engines

Readme

BetterMail 📧

npm version Build Status Coverage Status License: MIT

A world-class Node.js email sending library with support for multiple providers and template engines. Built with TypeScript for maximum type safety and developer experience.

✨ Features

  • 🚀 Multiple Providers: SMTP, Resend, SendGrid
  • 🎨 Template Engines: React, MJML, Handlebars
  • 🔒 Type Safe: Full TypeScript support
  • 🛡️ Error Handling: Comprehensive error management
  • 📝 Logging: Built-in logging with configurable levels
  • Performance: Optimized for high-volume sending
  • 🧪 Well Tested: 100% test coverage
  • 📦 Zero Dependencies: Minimal bundle size

🚀 Quick Start

Installation

npm install better-mail
# or
yarn add better-mail
# or
pnpm add better-mail

Basic Usage

import { createMailer, send } from 'better-mail';

// Configure BetterMail
createMailer({
  provider: 'resend', // or 'smtp', 'sendgrid'
  resend: {
    apiKey: 're_123...',
    from: '[email protected]'
  },
  templatesPath: './templates'
});

// Send an email
const result = await send({
  to: '[email protected]',
  template: 'welcome',
  templateEngine: 'react',
  variables: {
    name: 'John Doe',
    company: 'Your Company'
  }
});

console.log('Email sent:', result);

📚 Documentation

Configuration

BetterMail supports three email providers:

SMTP Configuration

createMailer({
  provider: 'smtp',
  smtp: {
    host: 'smtp.gmail.com',
    port: 587,
    user: '[email protected]',
    pass: 'your-app-password',
    from: '[email protected]'
  }
});

Resend Configuration

createMailer({
  provider: 'resend',
  resend: {
    apiKey: 're_123...',
    from: '[email protected]'
  }
});

SendGrid Configuration

createMailer({
  provider: 'sendgrid',
  sendgrid: {
    apiKey: 'SG.123...',
    from: '[email protected]'
  }
});

Template Engines

React Templates

// templates/welcome.tsx
import * as React from 'react';

export const subject = ({ name }: { name: string }) =>
  `Welcome to BetterMail, ${name}!`;

export const text = ({ name, company }: { name: string; company: string }) =>
  `Hi ${name},\n\nWelcome to ${company}! We're excited to have you on board.`;

export default function Welcome({ name, company }: { name: string; company: string }) {
  return (
    <html>
      <body>
        <h1>Welcome, {name}!</h1>
        <p>Thanks for joining {company}!</p>
      </body>
    </html>
  );
}

MJML Templates

<!-- templates/newsletter.mjml -->
<mjml>
  <mj-body>
    <mj-section>
      <mj-column>
        <mj-text>Hello {{name}}!</mj-text>
        <mj-text>Welcome to {{company}}!</mj-text>
      </mj-column>
    </mj-section>
  </mj-body>
</mjml>

Handlebars Templates

<!-- templates/invoice.hbs -->
<h1>Invoice #{{invoiceNumber}}</h1>
<p>Dear {{customerName}},</p>
<p>Thank you for your business!</p>
<p>Amount: ${{amount}}</p>

Advanced Usage

Multiple Recipients

await send({
  to: ['[email protected]', '[email protected]'],
  cc: '[email protected]',
  bcc: '[email protected]',
  template: 'newsletter',
  variables: { name: 'Subscribers' }
});

Attachments

await send({
  to: '[email protected]',
  template: 'invoice',
  variables: { invoiceNumber: 'INV-001' },
  attachments: [
    {
      filename: 'invoice.pdf',
      path: './invoices/invoice-001.pdf'
    }
  ]
});

Error Handling

import { BetterMailError, ValidationError, ProviderError } from 'better-mail';

try {
  await send({ /* ... */ });
} catch (error) {
  if (error instanceof ValidationError) {
    console.log('Validation failed:', error.message);
  } else if (error instanceof ProviderError) {
    console.log('Provider error:', error.message);
  } else if (error instanceof BetterMailError) {
    console.log('BetterMail error:', error.message);
  }
}

🛠️ Development

Prerequisites

  • Node.js 18+
  • npm/yarn/pnpm

Setup

git clone https://github.com/your-username/better-mail.git
cd better-mail
npm install

Scripts

npm run dev:full    # Lint + Format + Test
npm run build       # Build the package
npm run test        # Run tests
npm run lint        # Check code quality
npm run format      # Format code

Testing

npm test                    # Run all tests
npm run test:watch         # Watch mode
npm run test:coverage      # Coverage report

📊 API Reference

createMailer(config: MailerConfig)

Configures BetterMail with provider and template settings.

send(options: SendOptions): Promise<EmailResult>

Sends an email using the configured provider and template.

Types

interface MailerConfig {
  provider: 'smtp' | 'resend' | 'sendgrid';
  templates?: TemplateConfig;
  logging?: LoggingConfig;
  smtp?: SMTPConfig;
  resend?: ResendConfig;
  sendgrid?: SendGridConfig;
}

interface SendOptions {
  to: string | string[];
  template: string;
  templateEngine?: 'react' | 'mjml' | 'handlebars';
  variables: Record<string, unknown>;
  cc?: string | string[];
  bcc?: string | string[];
  attachments?: EmailAttachment[];
}

🤝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

  • Resend for their excellent email API
  • SendGrid for their reliable email service
  • React Email for their amazing React email components
  • MJML for their responsive email framework

📞 Support