Skip to content

Instantly share code, notes, and snippets.

@Corfucinas
Created November 9, 2023 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Corfucinas/a2a55fd5185c8a22e89ba657c97ef0c7 to your computer and use it in GitHub Desktop.
Save Corfucinas/a2a55fd5185c8a22e89ba657c97ef0c7 to your computer and use it in GitHub Desktop.
Cloudflare DKMI (Mailchannels)
// Taken from https://support.mailchannels.com/hc/en-us/articles/7122849237389
// In case you're using Godaddy as a registar
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
let send_request = new Request("https://api.mailchannels.net/tx/v1/send", {
"method": "POST",
"headers": {
"content-type": "application/json",
},
"body": JSON.stringify({
"personalizations": [{
"to": [ {"email": "test@example.com",
"name": "Test Recipient"}],
"dkim_domain": "example.com",
"dkim_selector": "mcdkim",
"dkim_private_key": "<base64 encoded private key>"
}],
"from": {
"email": "sender@example.com",
"name": "Test Sender",
},
"subject": "Test Subject",
"content": [{
"type": "text/plain",
"value": "Test message content\n\n" + content,
}],
}),
});
let respContent = "";
// only send the mail on "POST", to avoid spiders, etc.
if( request.method == "POST" ) {
const resp = await fetch(send_request);
const respText = await resp.text();
respContent = resp.status + " " + resp.statusText + "\n\n" + respText;
}
let htmlContent = "<html><head></head><body><pre>" +
"</pre><p>Click to send message: <form method="post"><input type="submit" value="Send"/></form></p>" +
"<pre>" + respContent + "</pre>" +
"</body></html>";
return new Response(htmlContent, {
headers: { "content-type": "text/html" },
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment