Skip to content

SupersaasHQ/useEmail

Repository files navigation

Supersaas/UseEmail

npm version npm downloads

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

  1. Resend
  2. SendGrid
  3. Postmark
  4. Plunk
  5. 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

local 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

License

Published under the MIT license. Made by community 💛