Skip to content

Mail Setup

SMTP

Cloudflare

cloudflare-worker.js
js
export default {
  async fetch(request) {
    const MAIL_AUTH_HEADER_KEY = "X-Mail-AUTHKEY";
    const psk = request.headers.get(MAIL_AUTH_HEADER_KEY);

    if (psk !== MAIL_AUTH_KEY) {
      // Incorrect key supplied. Reject the request.
      return new Response("Sorry, you have supplied an invalid key.", {
        status: 403,
      });
    }

    const contentType = request.headers.get("content-type");
    var body;
    if (contentType && contentType.includes("application/json")) {
      body = await request.json();
    } else {
      return new Response("Invalid parameters", { status: 400 });
    }

    const recipients = body["recipients"];
    const subject = body["subject"];
    const mailContent = body["content"];

    let send_request = new Request('https://api.mailchannels.net/tx/v1/send', {
      method: 'POST',
      headers: {
        'content-type': 'application/json',
      },
      body: JSON.stringify({
        personalizations: [
          {
            to: recipients,
          },
        ],
        from: {
          email: '[email protected]',
          name: 'SideStore Connect',
        },
        subject: subject,
        content: [
          mailContent,
        ],
      }),
    });

    const response = await fetch(send_request);
    const responseText = await resp.text();

    let responseContent = JSON.stringify({
        status: response.status,
        statusText: response.statusText,
        text: responseText,
    });

    return new Response(responseContent, { status: response.status, headers: new Headers({ "Content-Type": "application/json" }) });
  },
}
export default {
  async fetch(request) {
    const MAIL_AUTH_HEADER_KEY = "X-Mail-AUTHKEY";
    const psk = request.headers.get(MAIL_AUTH_HEADER_KEY);

    if (psk !== MAIL_AUTH_KEY) {
      // Incorrect key supplied. Reject the request.
      return new Response("Sorry, you have supplied an invalid key.", {
        status: 403,
      });
    }

    const contentType = request.headers.get("content-type");
    var body;
    if (contentType && contentType.includes("application/json")) {
      body = await request.json();
    } else {
      return new Response("Invalid parameters", { status: 400 });
    }

    const recipients = body["recipients"];
    const subject = body["subject"];
    const mailContent = body["content"];

    let send_request = new Request('https://api.mailchannels.net/tx/v1/send', {
      method: 'POST',
      headers: {
        'content-type': 'application/json',
      },
      body: JSON.stringify({
        personalizations: [
          {
            to: recipients,
          },
        ],
        from: {
          email: '[email protected]',
          name: 'SideStore Connect',
        },
        subject: subject,
        content: [
          mailContent,
        ],
      }),
    });

    const response = await fetch(send_request);
    const responseText = await resp.text();

    let responseContent = JSON.stringify({
        status: response.status,
        statusText: response.statusText,
        text: responseText,
    });

    return new Response(responseContent, { status: response.status, headers: new Headers({ "Content-Type": "application/json" }) });
  },
}

MAIL_AUTH_KEY: Pre-shared key, stored as an encrypted Cloudflare Worker variable.

Copyright © 2024 SideStore Connect. All Rights Reserved.