Skip to content
项目

关于

An SMTP library that helps you send email in Cloudflare Workers.

效率与自动化

这些数字如何统计

可以做什么

  • 🚀 Completely built on the Cloudflare Workers runtime with no other dependencies
  • 📝 Full TypeScript type support
  • 📧 Supports sending plain text and HTML emails with attachments
  • 🔒 Supports multiple SMTP authentication methods: plain, login, and CRAM-MD5

实测星标增长

24小时
+1
7天
+2
15天
+5
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README_zh-CN.md

原文

Worker Mailer

English | 简体中文

npm version License: MIT

Worker Mailer 是一个运行在 Cloudflare Workers 上的 SMTP 客户端库,通过 Cloudflare TCP Sockets 实现且不依赖任何其他第三方库。

特性

  • 🚀 完全基于 Cloudflare Workers 运行时,无第三方依赖
  • 📝 完整的 TypeScript 类型支持
  • 📧 支持纯文本和 HTML 邮件发送
  • 🔒 支持多种 SMTP 认证方式:plain、login 和 CRAM-MD5
  • 👥 丰富的收件人选项:收件人、抄送、密送和回复地址
  • 🎨 支持自定义邮件头部

💬 在用 worker-mailer? 花 1 分钟告诉我你在做什么:问卷。这会影响我接下来做什么。

🧪 先测一下 SMTP 配置: smtptest.dev 是基于 worker-mailer 的免费在线 SMTP 测试工具,跑的就是 Worker 里的同一套连接流程,会告诉你卡在哪一步(连接、TLS、登录还是发送)。

目录

安装

npm i worker-mailer

快速开始

  1. 配置 wrangler.toml:
compatibility_flags = ["nodejs_compat"]
# or compatibility_flags = ["nodejs_compat_v2"]
  1. 在代码中使用:
import { WorkerMailer } from 'worker-mailer'

// 连接到 SMTP 服务器
const mailer = await WorkerMailer.connect({
  credentials: {
    username: '[email protected]',
    password: 'password',
  },
  authType: 'plain',
  host: 'smtp.acme.com',
  port: 587,
  secure: true,
})

// 发送邮件
await mailer.send({
  from: { name: 'Bob', email: '[email protected]' },
  to: { name: 'Alice', email: '[email protected]' },
  subject: '来自 Worker Mailer 的问候',
  text: '这是一条纯文本消息',
  html: '<h1>你好</h1><p>这是一条 HTML 消息</p>',
})

API 参考

WorkerMailer.connect(options)

创建一个新的 SMTP 连接。

type WorkerMailerOptions = {
  host: string // SMTP 服务器主机名
  port: number // SMTP 服务器端口(通常是 587 或 465)
  secure?: boolean // 使用 TLS(默认:false)
  startTls?: boolean // 升级到 TLS 如果 SMTP 服务器支持(默认:true)
  credentials?: {
    // SMTP 认证凭据
    username: string
    password: string
  }
  authType?:
    | 'plain'
    | 'login'
    | 'cram-md5'
    | Array<'plain' | 'login' | 'cram-md5'>
  logLevel?: LogLevel // 日志级别(默认:LogLevel.INFO)
  socketTimeoutMs?: number // Socket 超时时间(毫秒)
  responseTimeoutMs?: number // 服务器响应超时时间(毫秒)
  name?: string // EHLO/HELO 时发送的客户端主机名(默认:[127.0.0.1])
}

mailer.send(options)

发送邮件。

type EmailOptions = {
  from:
    | string
    | {
        // 发件人邮箱
        name?: string
        email: string
      }
  to:
    | string
    | string[]
    | {
        // 收件人
        name?: string
        email: string
      }
    | Array<{ name?: string; email: string }>
  reply?:
    | string
    | {
        // 回复地址
        name?: string
        email: string
      }
  cc?:
    | string
    | string[]
    | {
        // 抄送收件人
        name?: string
        email: string
      }
    | Array<{ name?: string; email: string }>
  bcc?:
    | string
    | string[]
    | {
        // 密送收件人
        name?: string
        email: string
      }
    | Array<{ name?: string; email: string }>
  subject: string // 邮件主题
  text?: string // 纯文本内容
  html?: string // HTML 内容
  headers?: Record<string, string> // 自定义邮件头部
}

静态方法:WorkerMailer.send()

发送单次邮件,不保持连接。

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

使用限制

  • 端口限制: Cloudflare Workers 禁用 25 端口的出站连接,您无法在 25 端口提交邮件,但是主流的 587 和 465 等端口是支持的
  • 连接限制: 每个 Worker 实例对并发 TCP 连接数有限制,请确保在使用完毕后正确关闭连接

参与贡献

我们欢迎社区的贡献!以下是参与贡献的指南:

开发环境设置

  1. Fork 并克隆仓库
  2. 安装依赖:
    pnpm install
    
  3. 为您的功能/修复创建新分支:
    git checkout -b feature/your-feature-name
    
  4. 用户可感知的改动需运行 pnpm change 记录变更

发版

develop 上有待发布的变更时,每次推送都会自动创建或更新一个合并到 main 的 release: vX.Y.Z PR,其中包含版本号和 CHANGELOG 的更新。合并该 PR 后会自动发布到 npm、创建 GitHub Release 并同步 develop。

测试

  1. 单元测试:
    pnpm test
    
  2. 集成测试,连接本地的 Mailpit SMTP 服务器:
    docker run -d -p 1025:1025 -p 8025:8025 -e MP_SMTP_AUTH_ACCEPT_ANY=1 -e MP_SMTP_AUTH_ALLOW_INSECURE=1 axllent/mailpit
    pnpm test:integration
    
  3. 手动测试,连接真实的 SMTP 服务器:
    pnpm dlx wrangler dev ./test/worker.ts
    
    Then, send a POST request to http://127.0.0.1:8787 with the following JSON body:
    {
      "config": {
        "credentials": {
          "username": "[email protected]",
          "password": "xxxx"
        },
        "authType": "plain",
        "host": "smtp.acme.com",
        "port": 587,
        "secure": false,
        "startTls": true
      },
      "email": {
        "from": "[email protected]",
        "to": "[email protected]",
        "subject": "Test Email",
        "text": "Hello World"
      }
    }
    

Pull Request 流程

对于重大更改,请先开启一个 issue 讨论您想要改变的内容。

  1. 更新文档以反映任何更改
  2. 根据需要添加或更新测试
  3. 确保所有测试通过
  4. 如适用,更新更新日志
  5. 提交 pull request,并清晰描述您的更改

报告问题

报告问题时,请包含:

  • 问题的清晰描述
  • 复现问题的步骤
  • 预期行为与实际行为
  • 您使用的 worker-mailer 版本
  • 任何相关的代码片段或错误消息

许可证

本项目基于 MIT 许可证开源。

探索相关项目

加载中…