Resend Cloudflare Worker Service

in cloudflare •  3 months ago 

中文版在文末。 Chinese Version Below.

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 <[email protected]>"
### 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: '<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.

Sponsor

Donation ways:

License

MIT

Repo Link: https://github.com/willin/resend-cloudflare-service-worker

Resend Cloudflare Worker Service

中文版 / English

准备工作

  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 <[email protected]>"
### 发送测试邮件接收的地址
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: '<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 邮箱发送一封测试邮件。

赞助

如果您对本项目感兴趣,可以通过以下方式支持我:

  • 关注我的 Github 账号:@willin github
  • 参与 爱发电 计划
  • 支付宝或微信扫码打赏
  • ETH: 0x6D877f96198E55085CEb56097B099cc7bb814263

许可证

MIT

项目仓库: https://github.com/willin/resend-cloudflare-service-worker

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!