This worker let you send email via MailChannels from Cloudflare Workers.
- Fork this repo
- Clone to your workspace
- Setup Cloudflare DMARC Management for your domain name.
- Set
DKIM_PRIVATE_KEY
secret:
npx wrangler secret put DKIM_PRIVATE_KEY
- Deploy:
npm run deploy
- Bind this worker to another Workers or Pages with name
MAIL
or anything. - To send email
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: '<your-domain-name>',
from: { name: '<sender-name>', email: 'noreply@<your-domain-name>' },
to: [{ name: email.split('@')[0], email }],
subject: 'Hello World!',
content: [{ type: 'text/html', value: 'Hello World!' }],
})
);