
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
better-mail
Advanced tools
A world-class Node.js email sending library with support for multiple providers and template engines
A world-class Node.js email sending library with support for multiple providers and template engines
Documentation • Examples • GitHub • Discord
Feature | Description |
---|---|
🚀 Multiple Providers | Send emails via SMTP, Resend, or SendGrid |
🎨 Template Engines | Use React, MJML, or Handlebars for beautiful emails |
🔒 TypeScript First | Fully typed for better developer experience |
🛡️ Robust Error Handling | Custom error classes for clear debugging |
🔇 Silent by Default | No console output unless explicitly enabled |
🎯 Security Focused | Regular security audits and vulnerability scanning |
📊 Comprehensive Testing | 80%+ test coverage with unit and integration tests |
🏗️ Modern Development | ESLint, Prettier, Husky, and conventional commits |
npm install better-mail
import { createMailer, send } from 'better-mail';
// Initialize with your preferred provider
createMailer({
provider: 'smtp',
smtp: {
host: process.env.SMTP_HOST!,
port: Number(process.env.SMTP_PORT),
user: process.env.SMTP_USER!,
pass: process.env.SMTP_PASS!,
from: process.env.SMTP_FROM!,
},
});
// Send an email
await send({
to: 'recipient@example.com',
template: 'welcome',
templateEngine: 'react',
variables: { name: 'Alice' },
});
❌ NO - This library is NOT compatible with React apps running in the browser.
BetterMail is a Node.js server-side library designed for:
nodemailer
, winston
, etc.// Frontend (React app)
const sendWelcomeEmail = async userData => {
const response = await fetch('/api/send-welcome-email', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(userData),
});
return response.json();
};
// Backend (Node.js API)
import { createMailer, send } from 'better-mail';
createMailer({ provider: 'smtp' /* config */ });
app.post('/api/send-welcome-email', async (req, res) => {
try {
await send({
to: req.body.email,
template: 'welcome',
templateEngine: 'react',
variables: req.body,
});
res.json({ success: true });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
Create a .env
file in your project root:
# SMTP Configuration
SMTP_HOST=smtp.example.com
SMTP_PORT=587
SMTP_USER=your-user
SMTP_PASS=your-pass
SMTP_FROM=from@example.com
# Resend Configuration
RESEND_API_KEY=re_123
RESEND_FROM=from@example.com
# SendGrid Configuration
SENDGRID_API_KEY=SG.123
SENDGRID_FROM=from@example.com
import { createMailer } from 'better-mail';
// SMTP
createMailer({
provider: 'smtp',
smtp: {
host: 'smtp.example.com',
port: 587,
user: 'user@example.com',
pass: 'password',
from: 'from@example.com',
},
});
// Resend
createMailer({
provider: 'resend',
resend: {
apiKey: 're_123',
from: 'from@example.com',
},
});
// SendGrid
createMailer({
provider: 'sendgrid',
sendgrid: {
apiKey: 'SG.123',
from: 'from@example.com',
},
});
BetterMail is silent by default - no console output unless explicitly enabled:
// Silent (default) - no console output
createMailer({
provider: 'smtp',
smtp: {
/* config */
},
logging: {
silent: true, // or level: 'silent'
},
});
// Enable logging for debugging
createMailer({
provider: 'smtp',
smtp: {
/* config */
},
logging: {
level: 'debug', // 'error' | 'warn' | 'info' | 'debug'
},
});
// Disable all logging (including file logs)
createMailer({
provider: 'smtp',
smtp: {
/* config */
},
logging: {
silent: true,
},
});
Create templates/welcome.tsx
:
import * as React from 'react';
export const subject = ({ name }: { name: string }) =>
`Welcome to BetterMail, ${name}!`;
export const text = ({ name }: { name: string }) =>
`Hi ${name},\n\nWelcome to BetterMail!`;
export function welcome({ name }: { name: string }) {
return (
<html>
<body style={{ fontFamily: 'Arial, sans-serif' }}>
<h1>Hi {name} 👋</h1>
<p>Welcome to BetterMail!</p>
</body>
</html>
);
}
export default welcome;
BetterMail includes a beautiful, customizable welcome template:
// Use the built-in welcome template
await send({
to: 'user@example.com',
template: 'default-welcome',
templateEngine: 'react',
variables: {
name: 'John Doe',
company: 'MyApp',
features: ['Feature 1', 'Feature 2', 'Feature 3'],
primaryColor: '#3B82F6', // Customize colors
secondaryColor: '#1F2937',
accentColor: '#10B981',
},
});
Create templates/newsletter.mjml
:
<mjml>
<mj-body>
<mj-section>
<mj-column>
<mj-text font-size="20px">Hi {{name}} 👋</mj-text>
<mj-text>Check out our latest newsletter!</mj-text>
</mj-column>
</mj-section>
</mj-body>
</mjml>
Create templates/invoice.hbs
:
<html>
<body style="font-family: Arial, sans-serif;">
<h1>Invoice for {{customerName}}</h1>
<p>Amount: {{amount}}</p>
<p>Due Date: {{dueDate}}</p>
</body>
</html>
# Clone the repository
git clone https://github.com/better-mail/better-mail.git
cd better-mail
# Install dependencies
npm install
# Set up Git hooks
npm run prepare
# Build the project
npm run build
# Run tests
npm test
npm run build # Build the project
npm run test # Run tests
npm run test:watch # Run tests in watch mode
npm run test:coverage # Run tests with coverage
npm run lint # Run ESLint
npm run lint:fix # Fix ESLint issues
npm run format # Format code with Prettier
npm run docs # Generate documentation
npm run example:welcome # Run welcome template example
We welcome contributions! Please see our Contributing Guidelines for details.
git checkout -b feature/amazing-feature
)git commit -m 'Add amazing feature'
)git push origin feature/amazing-feature
)This project is licensed under the MIT License - see the LICENSE file for details.
FAQs
A world-class Node.js email sending library with support for multiple providers and template engines
The npm package better-mail receives a total of 0 weekly downloads. As such, better-mail popularity was classified as not popular.
We found that better-mail demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.