https://github.com/chientrm/cf-mailchannels
Cloudflare MailChannels Worker
https://github.com/chientrm/cf-mailchannels
Last synced: 8 months ago
JSON representation
Cloudflare MailChannels Worker
- Host: GitHub
- URL: https://github.com/chientrm/cf-mailchannels
- Owner: chientrm
- Created: 2024-02-03T20:33:05.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-02-03T20:54:58.000Z (over 2 years ago)
- Last Synced: 2024-12-02T10:14:56.412Z (over 1 year ago)
- Language: TypeScript
- Size: 16.6 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# cf-mailchannels
This worker let you send email via MailChannels from Cloudflare Workers.
## Setup
1. Fork this repo
2. Clone to your workspace
3. Setup [Cloudflare DMARC Management](https://developers.cloudflare.com/dmarc-management/) for your domain name.
4. Set `DKIM_PRIVATE_KEY` secret:
```bash
npx wrangler secret put DKIM_PRIVATE_KEY
```
5. Deploy:
```bash
npm run deploy
```
## How to use
1. Bind this worker to another Workers or Pages with name `MAIL` or anything.
2. To send email
```ts
const sendMail = (
fetcher: Fetcher,
body: {
from: { name: string; email: string };
to: { name: string; email: string }[];
dkim_domain: string;
subject: string;
content: { type: 'text/plain' | 'text/html'; value: string }[];
}
) =>
fetcher.fetch('http://whatever.fake/send', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
// Example usage
waitUntil(
sendMail(platform.env.MAIL, {
dkim_domain: '',
from: { name: '', email: 'noreply@' },
to: [{ name: email.split('@')[0], email }],
subject: 'Hello World!',
content: [{ type: 'text/html', value: 'Hello World!' }],
})
);
```