MailChannels and Turnstile validation not working when combined 405 error

This is the code in _middleware.ts Each of the blocks is working separately (when I comment turnstile block, emails are being sent, when I comment out mailchannel code, turnstile validation works) but when I uncomment both blocks I get 405 error. I can see all log messages.
import mailChannelsPlugin from "@cloudflare/pages-plugin-mailchannels";

const handleMailChannelsPlugin = async (context) => {
//Turnstile
const request = context.request;
if (request.method === 'POST') {
console.log("enter hmp");
const body = await request.formData();
// Turnstile injects a token in "cf-turnstile-response".
const token = body.get('cf-turnstile-response');
const ip = request.headers.get('CF-Connecting-IP');

// Validate the token by calling the "/siteverify" API.
let formData = new FormData();
formData.append('secret', '***');
formData.append('response', token);
formData.append('remoteip', ip);

const result = await fetch('https://challenges.cloudflare.com/turnstile/v0/siteverify', {
body: formData,
method: 'POST',
});
const outcome = await result.json();
if (!outcome.success) {
console.log("turnstile invalid");
return new Response('The provided Turnstile token was not valid!');
}
console.log("turnstile valid");
}
console.log("return plugin");

//MailChannel
return mailChannelsPlugin({
personalizations: [
{
to: [{ name: "XXX", email: "****" }],
},
],
from: {
name: "*****",
email: "*****",
},
subject: "New Contact",
respondWith: () => {
console.log('sending response');
return new Response(
`Thank you for showing interest in our product.`
);
},
})(context);
};

export const onRequest = [handleMailChannelsPlugin];
import mailChannelsPlugin from "@cloudflare/pages-plugin-mailchannels";

const handleMailChannelsPlugin = async (context) => {
//Turnstile
const request = context.request;
if (request.method === 'POST') {
console.log("enter hmp");
const body = await request.formData();
// Turnstile injects a token in "cf-turnstile-response".
const token = body.get('cf-turnstile-response');
const ip = request.headers.get('CF-Connecting-IP');

// Validate the token by calling the "/siteverify" API.
let formData = new FormData();
formData.append('secret', '***');
formData.append('response', token);
formData.append('remoteip', ip);

const result = await fetch('https://challenges.cloudflare.com/turnstile/v0/siteverify', {
body: formData,
method: 'POST',
});
const outcome = await result.json();
if (!outcome.success) {
console.log("turnstile invalid");
return new Response('The provided Turnstile token was not valid!');
}
console.log("turnstile valid");
}
console.log("return plugin");

//MailChannel
return mailChannelsPlugin({
personalizations: [
{
to: [{ name: "XXX", email: "****" }],
},
],
from: {
name: "*****",
email: "*****",
},
subject: "New Contact",
respondWith: () => {
console.log('sending response');
return new Response(
`Thank you for showing interest in our product.`
);
},
})(context);
};

export const onRequest = [handleMailChannelsPlugin];
devenglish.pages.dev
2 Replies
skoogeer
skoogeer•2y ago
What is the content of the 405?
Daniel
DanielOP•2y ago
Method Not Allowed In the end I dropped middleware and wrote a function, works fine. 😕
Want results from more Discord servers?
Add your server