use-email
v0.0.8
Published
<!-- automd:badges color=yellow -->
Downloads
60
Readme
Supersaas/UseEmail
A unified TypeScript hook for sending emails across multiple providers with a single interface. This package simplifies email sending operations by providing a consistent API regardless of the underlying email service provider.
Features
Unified interface for multiple email providers TypeScript support for enhanced developer experience Works with Node.js, Bun, Deno, and Cloudflare Workers Easy to switch between providers without changing your code Supports modern email providers
Supported Providers
- Resend
- SendGrid
- Postmark
- Plunk
- Mailgun
Installation You can install the package using your preferred package manager:
import { useEmail } from "use-email";
const emailService = useEmail("resend"); // Choose your provider
await emailService.send({
from: "[email protected]",
to: "[email protected]",
subject: "Hello from use-email!",
text: "This is a test email sent using use-email package.",
});
Switching Providers
const resendService = useEmail("resend");
const sendgridService = useEmail("sendgrid");
const postmarkService = useEmail("postmark");
const plunkService = useEmail("plunk");
const mailgunService = useEmail("mailgun");
Email Options
The send method accepts an EmailOptions object with the following properties:
type EmailOptions = {
from: string;
to: string | string[];
subject: string;
html?: string;
text?: string;
};
Error Handling
The package throws errors for common issues such as missing API keys or required email fields. Always wrap your email sending code in a try-catch block:
try {
await emailService.send({
from: "[email protected]",
to: "[email protected]",
subject: "Test Email",
text: "This is a test.",
});
console.log("Email sent successfully");
} catch (error) {
console.error("Failed to send email:", error);
}
TypeScript Support
This package is written in TypeScript and provides type definitions out of the box. You'll get full IntelliSense and type checking when using it in a TypeScript project.
Installation
You can install the package using your preferred package manager:
# ✨ Auto-detect
npx nypm install use-email
# npm
npm install use-email
# yarn
yarn add use-email
# pnpm
pnpm install use-email
# bun
bun install use-email
Import:
ESM (Node.js, Bun)
import {} from "pkg";
CommonJS (Legacy Node.js)
const {} = require("pkg");
CDN (Deno, Bun and Browsers)
import {} from "https://esm.sh/pkg";
Development
- Clone this repository
- Install latest LTS version of Node.js
- Enable Corepack using
corepack enable
- Install dependencies using
pnpm install
- Run interactive tests using
pnpm dev