Email worker to send email data via API using fetch post: too many redirect error

What is the name of the domain?

What is the issue you’re encountering

It shows error “Uncaught (in promise) TypeError: Too many redirects” in console

What are the steps to reproduce the issue?

Here is my piece of code:

export default {
  async email(message, env, ctx) {
    // Extract the sender and subject
    const sender = message.from;
    const headers = Object.fromEntries(message.headers);

    // Extract raw email content
    const rawEmail = await new Response(message.raw).text();

    // Prepare payload for the API
    const payload = JSON.stringify({
      subject: headers.subject,
      name: sender,
      email: sender,
      message: rawEmail,
    });

    // API Call with Basic Authentication
    const apiResponse = await fetch("https://example.com/api/inboxes/", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Authorization: "Basic secret",
      },
      body: payload,
    });

    // Check API response
    if (!apiResponse.ok) {
      console.error("API Call Failed. Status:", apiResponse.status);
      const errorText = await apiResponse.text();
      console.error("Error Response Body:", errorText);
      message.setReject("Failed to process email.");
    } else {
      console.log("Email processed and forwarded to API.");
    }
  },
};
1 Like

May I ask if the Worker code is running over a domain example.com or sub.example.com, which is active on Cloudflare, proxied :orange: and has got Flexible SSL selected under the SSL/TLS menu at Cloudflare dashboard? :thinking:

Yes @fritex, but it is Full (strict) instead of Flexible. Do you have an idea what is going on? thanks!

Would also love to know. Can anyone from the Cloudflare team comment on this?

Thanks.