本文主要是介绍Resend Cloudflare Worker Service,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
提供中文、英文两个版本。
目录
- 准备工作
- Worker / Pages 中使用
- 本地开发测试
- Prepare
- Use in Worker / Pages
- Local Development
代码仓库: https://github.com/willin/resend-cloudflare-service-worker
准备工作
Use this template
活 Fork 这个仓库来创建你的项目- 在项目
Settings
里设置Actions Secrets
,添加如下环境变量:
### 仅用于 Github Actions
CLOUDFLARE_ACCOUNT_ID="xxx"
CLOUDFLARE_API_TOKEN="xxx"### Resend 邮件发送服务,可自行注册免费发送
RESEND_API_KEY="re_xxxx"
### 发送邮件的用户名
EMAIL_USERNAME="No-Reply <no-reply@system.example.com>"
### 发送测试邮件接收的地址
TEST_EMAIL_ADDRESS="your@email.com"
Worker / Pages 中使用
在项目工程中修改 wrangler.toml
配置,添加:
[[services]]
binding = "RESEND"
service = "email-sender-worker"
然后在代码中使用:
await ctx.env.RESEND.sendEmail({// 或者不用数组,直接一个邮箱字符串to: ['address@example.com'],subject: 'Hello, World!',html: '<h1>Hello, World!</h1>'
}); // 返回值类型为 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
Use this template
to fork this repository to create your project- Set
Actions Secrets
in the projectSettings
, 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 <no-reply@system.example.com>"
### Address to receive test emails
TEST_EMAIL_ADDRESS="your@email.com"
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: ['address@example.com'], // or just a stringsubject: 'Hello, World!',html: '<h1>Hello, World!</h1>'
}); // 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.
这篇关于Resend Cloudflare Worker Service的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!