- Clone this repository
- Install the dependencies with
npm install
- Add a random
TOKEN
to thewrangler.toml
file (this will be used to authenticate your requests) - Deploy the worker with
npm run deploy
Or deploy directly to cloudflare
SPF is a DNS record that helps prevent email spoofing. You will need to add an SPF record to your domain to allow MailChannels to send emails on your behalf.
- Add a
TXT
record to your domain with the following values: - Name:@
- Value:v=spf1 a mx include:relay.mailchannels.net ~all
Once you have deployed this worker function to Cloudflare Workers, you can send emails by making a POST
request to the worker on the /api/email
endpoint with the following parameters:
- Note you need to pass an
Authorization
header with theTOKEN
you set in thewrangler.toml
file. Like the following:Authorization: TOKEN
The Most basic request would look like this:
{
"to": "[email protected]",
"from": "[email protected]",
"subject": "Hello World",
"text": "Hello World"
}
You can also send HTML emails by adding an html
parameter to the request. This can be used in conjunction with the text
parameter to send a multi-part email.
{
"to": "[email protected]",
"from": "[email protected]",
"subject": "Hello World",
"html": "<h1>Hello World</h1>"
}
You can also specify a sender and recipient name by adding a name
parameter to the request. This can be used in conjunction with the to
and from
parameters.
{
"to": { "email": "[email protected]", "name": "John Doe" },
"from": { "email": "[email protected]", "name": "Jane Doe" },
"subject": "Hello World",
"text": "Hello World"
}
You may also send to multiple recipients by passing an array of eamils, or an array of objects with email
and name
properties.
{
"to": [
"[email protected]",
"[email protected]"
],
"from": "[email protected]",
"subject": "Hello World",
"text": "Hello World"
}
or
{
"to": [
{ "email": "[email protected]", "name": "John Doe" },
{ "email": "[email protected]", "name": "Rose Doe" }
],
"from": "[email protected]",
"subject": "Hello World",
"text": "Hello World"
}
You can also send BCC and CC emails by passing an array of eamils, an object with email
and name
properties, or an array of either, similar to the to
parameter.
{
"to": "[email protected]",
"from": "[email protected]",
"subject": "Hello World",
"text": "Hello World",
"cc": [
"[email protected]",
"[email protected]"
],
"bcc": [
"[email protected]"
]
}
You can also specify a reply to email address by adding a replyTo
parameter to the request. Again, you can use an email string, an object with email
and name
properties, or an array of either.
{
"to": "[email protected]",
"from": "[email protected]",
"replyTo": "[email protected]",
"subject": "Hello World",
"text": "Hello World"
}