Skip to content

A Resend Email Service Worker For Cloudflare Workers/Pages Service Binding

License

Notifications You must be signed in to change notification settings

willin/resend-cloudflare-service-worker

Repository files navigation

Resend Cloudflare Worker Service

中文版 / English

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