提供中文、英文两个版本。
代码仓库: 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 "
### 发送测试邮件接收的地址
TEST_EMAIL_ADDRESS="[email protected]"
在项目工程中修改 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
邮箱发送一封测试邮件。
Use this template
to fork this repository to create your projectActions 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]"
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;
};
# 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.