- Clone this repository
- Install the dependencies with
npm install
- Use the command
npx wrangler secret put --env production TOKEN
to deploy a securely stored token to Cloudflare. With this command, you will be prompted to enter a random secret value, which will be used to authenticate your requests with the HTTPAuthorization
header as described below. You can also set this encrypted value directly in your Cloudflare dashboard. - 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
- Name:
This step is optional, but highly recommended. DKIM is a DNS record that helps prevent email spoofing. You may follow the steps listed in the MailChannels documentation to set up DKIM for your domain.
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 the secure token you deployed. Like the following:Authorization: TOKEN
Your api/email
request payload should be structured as follows:
{
"email": {
"from": "from@email.xyz",
"fromName": "John Doe",
"replyTo": [
"replyone@email.xyz",
"replytwo@email.xyz"
],
"subject": "email subject",
"text": "text body",
"html": "html body",
"recipients": {
"to": [
{
"name": "Recipient1",
"email": "abc12345"
},
{
"name": "Recipient2",
"email": "abc@pqr.xyz"
}
],
"cc": [
"recipient_cc1@email.xyz",
"recipient_cc2@email.xyz"
],
"bcc": [
"recipient_bcc1@email.xyz",
"recipient_bcc2@email.xyz"
]
}
},
"metadata": {
"campaignType": "PROMOTIONAL",
"custom": {
"key1": "val1",
"key2": "val2"
},
"timestamp": 1521012814,
"messageId": "message-id"
},
"version": "1.0"
}