// Cloudflare Pages Function // Deployed automatically as: POST /api/send-autoreply // // Sends an auto-reply email to the person who filled out the contact form, // using Resend (https://resend.com), from contact@wingsprotocol.com. // // The Resend API key is read from an environment variable (RESEND_API_KEY), // configured in the Cloudflare Pages dashboard — it is never stored in this file // or in any file served to the browser. export async function onRequestPost(context) { const { request, env } = context; let data; try { data = await request.json(); } catch (err) { return new Response(JSON.stringify({ success: false, error: "Invalid request body" }), { status: 400, headers: { "Content-Type": "application/json" } }); } const name = (data.name || "").toString().trim(); const email = (data.email || "").toString().trim(); // Basic validation — don't attempt to send if essentials are missing. if (!name || !email) { return new Response(JSON.stringify({ success: false, error: "Missing name or email" }), { status: 400, headers: { "Content-Type": "application/json" } }); } // Very basic email shape check to avoid sending to obviously invalid addresses. const emailLooksValid = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email); if (!emailLooksValid) { return new Response(JSON.stringify({ success: false, error: "Invalid email" }), { status: 400, headers: { "Content-Type": "application/json" } }); } const apiKey = env.RESEND_API_KEY; if (!apiKey) { return new Response(JSON.stringify({ success: false, error: "Server not configured" }), { status: 500, headers: { "Content-Type": "application/json" } }); } const safeName = name.replace(//g, ">"); const html = `

Hi ${safeName},

Thanks for reaching out to Wingsprotocol. We've received your message and a member of our team will get back to you shortly.

If your inquiry is urgent, feel free to reply directly to this email.

Best regards,
The Wingsprotocol Team

`; try { const resendResponse = await fetch("https://api.resend.com/emails", { method: "POST", headers: { "Authorization": `Bearer ${apiKey}`, "Content-Type": "application/json" }, body: JSON.stringify({ from: "Wingsprotocol ", to: [email], subject: "We've received your message — Wingsprotocol", html: html }) }); if (!resendResponse.ok) { const errText = await resendResponse.text(); return new Response(JSON.stringify({ success: false, error: errText }), { status: 502, headers: { "Content-Type": "application/json" } }); } return new Response(JSON.stringify({ success: true }), { status: 200, headers: { "Content-Type": "application/json" } }); } catch (err) { return new Response(JSON.stringify({ success: false, error: "Send failed" }), { status: 500, headers: { "Content-Type": "application/json" } }); } }