Resend Cloudflare Worker Service

提供中文、英文两个版本。

目录

    • 准备工作
    • Worker / Pages 中使用
    • 本地开发测试
    • Prepare
    • Use in Worker / Pages
    • Local Development

代码仓库: https://github.com/willin/resend-cloudflare-service-worker

准备工作

  1. Use this template 活 Fork 这个仓库来创建你的项目
  2. 在项目 Settings 里设置 Actions Secrets,添加如下环境变量:
### 仅用于 Github Actions
CLOUDFLARE_ACCOUNT_ID="xxx"
CLOUDFLARE_API_TOKEN="xxx"

### Resend 邮件发送服务,可自行注册免费发送
RESEND_API_KEY="re_xxxx"
### 发送邮件的用户名
EMAIL_USERNAME="No-Reply "
### 发送测试邮件接收的地址
TEST_EMAIL_ADDRESS="[email protected]"

Worker / Pages 中使用

在项目工程中修改 wrangler.toml 配置,添加:

[[services]]
binding = "RESEND"
service = "email-sender-worker"

然后在代码中使用:

await ctx.env.RESEND.sendEmail({
  // 或者不用数组,直接一个邮箱字符串
  to: ['[email protected]'],
  subject: 'Hello, World!',
  html: '

Hello, World!

'
}); // 返回值类型为 boolean

其中 sendEmail 的参数为 SendEmailOptions 类型:

type SendEmailProps = {
  to: string | string[];
  subject: string;
  html: string;
};

本地开发测试

# 使用 npm、yarn 或者 pnpm 也可以
bun install
bun run dev

然后访问 http://localhost:8787 ,会给 TEST_EMAIL_ADDRESS 邮箱发送一封测试邮件。

Prepare

  1. Use this template to fork this repository to create your project
  2. Set Actions Secrets in the project Settings, add the following environment variables:
### Only For Github Actions
CLOUDFLARE_ACCOUNT_ID="xxx"
CLOUDFLARE_API_TOKEN="xxx"

### Resend mail sending service, you can register for free sending
RESEND_API_KEY="re_xxxx"
### The username of the email sent
EMAIL_USERNAME="No-Reply "
### Address to receive test emails
TEST_EMAIL_ADDRESS="[email protected]"

Use in Worker / Pages

Modify the wrangler.toml configuration in the project, add:

[[services]]
binding = "RESEND"
service = "email-sender-worker"

Then use in the code:

await ctx.env.RESEND.sendEmail({
  to: ['[email protected]'], // or just a string
  subject: 'Hello, World!',
  html: '

Hello, World!

'
}); // returns b

Where the parameter of sendEmail is of type SendEmailOptions:

type SendEmailProps = {
  to: string | string[];
  subject: string;
  html: string;
};

Local Development

# npm, yarn, or pnpm
bun install
bun run dev

Then visit http://localhost:8787, a test email will be sent to TEST_EMAIL_ADDRESS mailbox.

你可能感兴趣的:(后端,restful,中间件,分布式,架构)