Do mailchannels work with Functions?

Hello there!

I have the following function:

export async function onRequestPost(context) {
  try {
    const { request } = context;

    const { name, email, subject, message } = await request.json();

    const resp = await fetch(
      new Request("https://api.mailchannels.net/tx/v1/send", {
        method: "POST",
        headers: {
          "content-type": "application/json",
        },
        body: JSON.stringify({
          personalizations: [
            {
              to: [
                {
                  email: "[email protected]",
                  name: "Name",
                },
              ],
            },
          ],
          from: {
            email: "[email protected]",
            name,
          },
          subject,
          content: [
            {
              type: "text/plain",
              value: `From: ${email}: ${message}`,
            },
          ],
        }),
      })
    );

    const respContent = resp.status + " " + resp.statusText;

    return new Response(
      JSON.stringify({
        success: true,
        data: {
          message: "Your message has been sent!",
          respContent,
        },
      }),
      {
        headers: {
          "content-type": "application/json",
          // Allow all cors
          "Access-Control-Allow-Origin": "*",
          // Allow all cors methods
          "Access-Control-Allow-Methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
          // Allow all cors headers
          "Access-Control-Allow-Headers": "*",
        },
      }
    );
  } catch (err) {
    return new Response(
      JSON.stringify({
        success: false,
        data: {
          message: "Something went wrong!",
          error: err.message,
        },
      }),
      {
        headers: {
          "content-type": "application/json",
          // Allow all cors
          "Access-Control-Allow-Origin": "*",
          // Allow all cors methods
          "Access-Control-Allow-Methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
          // Allow all cors headers
          "Access-Control-Allow-Headers": "*",
        },
      }
    );
  }
}

The output is always “Accepted”, but the email does not get sent. However, if I try the same on workers, the email indeed gets sent… Am I doing something wrong?

1 Like

Why is it even mentioned than?
https://developers.cloudflare.com/pages/platform/functions/plugins/mailchannels/

1 Like

Functions are Workers and come from the same IP addresses, so that isn’t what is causing issues for you.

1 Like

I have been trying to get this to work for some time. I have tried functions, workers nothing has worked. I always get the following back from mailchannels

401 Unauthorized

When you use an API key it is fine but then you need to buy at least their starter packages

At this point, i am convinced that mailchannels stopped. I am convinced at this point mailchannels blocked this free service and for some reason neither they or Cloudflare will admit this wasting a lot of developers time.

1 Like