+View sender worker code
+/**
+ * Welcome to Cloudflare Workers! This is your first worker.
+ *
+ * - Run "npm run dev" in your terminal to start a development server
+ * - Open a browser tab at http://localhost:8787/ to see your worker in action
+ * - Run "npm run deploy" to publish your worker
+ *
+ * Learn more at https://developers.cloudflare.com/workers/
+ */
+async function readRequestBody(request, env) {
+ const { headers } = request;
+ const contentType = headers.get("content-type") || "";
+ if (contentType.includes("application/json")) {
+ return JSON.stringify(await request.json());
+ } else if (contentType.includes("form")) {
+ const formData = await request.formData();
+ const body = {};
+ for (const entry of formData.entries()) {
+ body[entry[0]] = entry[1];
+ }
+ let data = JSON.parse(JSON.stringify(body));
+ let combine = `{"personalizations":[{"to":[{"email":"${data.to}","name":"${data.ton}"}],"dkim_domain":"${env.DKIM_DOMAIN}","dkim_selector":"${env.DKIM_SELECTOR}","dkim_private_key":"${env.DKIM_PRIVATE_KEY}"}],"from":{"email":"${data.from}","name":"${data.fromn}"},"reply_to":{"email":"${data.rep}","name":"${data.repn}"},"subject":"${data.sbj}","content":[{"type":"${data.type}","value":"${data.body}"}]}`;
+ return combine;
+ } else {
+ return '{"success":false}';
+ }
+}
+async function handleRequest(request, env) {
+ let start = Date.now();
+ let reqBody = await readRequestBody(request, env);
+ let send_request = new Request("https://api.mailchannels.net/tx/v1/send", {
+ method: "POST",
+ headers: {
+ "content-type": "application/json",
+ },
+ body: reqBody,
+ });
+ let resp = await fetch(send_request);
+ let respText = await resp.text();
+ let end = Date.now();
+ let total = end - start;
+ return new Response(respText, {
+ headers: {
+ "X-MC-Status": resp.status,
+ "X-Response-Time": total,
+ },
+ });
+}
+const htmlForm = `
+
+
+
+Submit your email
+
+
+(*) is required
+
+
+`;
+export default {
+ async fetch(request, env, ctx) {
+ const { url } = request;
+ if (url.includes("submit")) {
+ return new Response(htmlForm, {
+ headers: { "Content-Type": "text/html" },
+ });
+ }
+ if (request.method === "POST") {
+ return handleRequest(request, env);
+ } else if (request.method === "GET") {
+ return new Response(`The request was a GET`);
+ }
+ },
+};
+
+
+
+Continue to save and deploy.
+
+- In the worker's `Settings` -> `Variables`, add the following variables:
+
+| Variable Name | Description | Example |
+| ---------------- | ------------------------------------ | --------------------- |
+| DKIM_DOMAIN | Required, domain suffix | `vmail.dev` |
+| DKIM_PRIVATE_KEY | Required, DKIM private key | `MIIEpQIBAAKCAQEA...` |
+| DKIM_SELECTOR | Required, fixed value `mailchannels` | `mailchannels` |
+
+The `DKIM_PRIVATE_KEY` will be explained later on how to obtain it.
+
+Remember the address assigned to the worker, i.e., `https://