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 🙏

© 2024 – Pkg Stats / Ryan Hefner

worker-mailer

v1.0.1

Published

[English](./README.md) | [简体中文](./README_zh-CN.md)

Downloads

168

Readme

Worker Mailer

English | 简体中文

npm version License: MIT

Worker Mailer is an SMTP client that runs on Cloudflare Workers. It leverages Cloudflare TCP Sockets and doesn't rely on any other dependencies.

Features

  • 🚀 Completely built on the Cloudflare Workers runtime with no other dependencies
  • 📝 Full TypeScript type support
  • 📧 Supports sending plain text and HTML emails
  • 🔒 Supports multiple SMTP authentication methods: plain, login, and CRAM-MD5
  • 👥 Rich recipient options: TO, CC, BCC, and Reply-To
  • 🎨 Custom email headers support

Table of Contents

Installation

npm i worker-mailer

Quick Start

  1. Configure your wrangler.toml:
compatibility_flags = ["nodejs_compat"]
# or compatibility_flags = ["nodejs_compat_v2"]
  1. Use in your code:
import { WorkerMailer } from 'worker-mailer'

// Connect to SMTP server
const mailer = await WorkerMailer.connect({
  credentials: {
    username: '[email protected]',
    password: 'password',
  },
  authType: 'plain',
  host: 'smtp.acme.com',
  port: 587,
  secure: true,
})

// Send email
await mailer.send({
  from: { name: 'Bob', email: '[email protected]' },
  to: { name: 'Alice', email: '[email protected]' },
  subject: 'Hello from Worker Mailer',
  text: 'This is a plain text message',
  html: '<h1>Hello</h1><p>This is an HTML message</p>'
})

API Reference

WorkerMailer.connect(options)

Creates a new SMTP connection.

type WorkerMailerOptions = {
  host: string;              // SMTP server hostname
  port: number;              // SMTP server port (usually 587 or 465)
  secure?: boolean;          // Use TLS (default: false)
  credentials?: {            // SMTP authentication credentials
    username: string;
    password: string;
  };
  authType?: 'plain' | 'login' | 'cram-md5' | Array<'plain' | 'login' | 'cram-md5'>;
  logLevel?: LogLevel;       // Logging level (default: LogLevel.INFO)
  socketTimeoutMs?: number;  // Socket timeout in milliseconds
  responseTimeoutMs?: number;// Server response timeout in milliseconds
}

mailer.send(options)

Sends an email.

type EmailOptions = {
  from: string | {          // Sender's email
    name?: string;
    email: string;
  };
  to: string | string[] | { // Recipients (TO)
    name?: string;
    email: string;
  } | Array<{ name?: string; email: string }>;
  reply?: string | {        // Reply-To address
    name?: string;
    email: string;
  };
  cc?: string | string[] | { // Carbon Copy recipients
    name?: string;
    email: string;
  } | Array<{ name?: string; email: string }>;
  bcc?: string | string[] | { // Blind Carbon Copy recipients
    name?: string;
    email: string;
  } | Array<{ name?: string; email: string }>;
  subject: string;          // Email subject
  text?: string;            // Plain text content
  html?: string;            // HTML content
  headers?: Record<string, string>; // Custom email headers
}

Static Method: WorkerMailer.send()

Send a one-off email without maintaining the connection.

await WorkerMailer.send(
  {
    // WorkerMailerOptions
    host: 'smtp.acme.com',
    port: 587,
    credentials: {
      username: 'user',
      password: 'pass'
    }
  },
  {
    // EmailOptions
    from: '[email protected]',
    to: '[email protected]',
    subject: 'Test',
    text: 'Hello'
  }
)

Limitations

  • Port Restrictions: Cloudflare Workers cannot make outbound connections on port 25. You won't be able to send emails via port 25, but common ports like 587 and 465 are supported.
  • Connection Limits: Each Worker instance has a limit on the number of concurrent TCP connections. Make sure to properly close connections when done.

Contributing

We welcome your contributions! If you encounter any issues or have suggestions while using this library, feel free to open an issue on our GitHub repository.

License

This project is licensed under the MIT License.