Wrap static forms plugin to retrieve email in context
All checks were successful
release / Publish to Cloudflare Pages (push) Successful in 1m20s

This commit is contained in:
2025-02-03 09:55:18 +10:30
parent f2227f673e
commit 8cf55aa0eb

View File

@@ -1,7 +1,11 @@
import staticFormsPlugin from "@cloudflare/pages-plugin-static-forms";
import { EmailMessage } from "cloudflare:email";
export const onRequest: PagesFunction = staticFormsPlugin({
const formatEmptyString = (s: string) => s ?? "Not Specified";
export const onRequest: PagesFunction = (context) => {
// Wrap static forms plugin so we can extract the env to use email routing
return staticFormsPlugin({
respondWith: async ({ formData }) => {
const fullName = formData.get("name");
const organisation = formData.get("org") ?? "Unknown Organisation";
@@ -11,26 +15,6 @@ export const onRequest: PagesFunction = staticFormsPlugin({
// Must have some kind of identifiable information for me to actually care about them.
if ((fullName || email) && message) {
// const emailMessage = createMimeMessage();
// emailMessage.setSender({
// name: "Michael Pivato Contact Form",
// addr: "contact@michaelpivato.dev",
// });
// emailMessage.setRecipient("contact@michaelpivato.dev");
// emailMessage.setSubject(`Message from ${fullName ?? email}`);
// emailMessage.addMessage({
// contentType: "text/plain",
// data: `You've received a new message from ${fullName ?? email}.
// Full Name: ${formatEmptyString(fullName)}
// Organisation: ${formatEmptyString(organisation)}
// Email: ${formatEmptyString(email)}
// Mobile: ${formatEmptyString(mobile)}
// Message:
// ${message}
// `,
// });
const rawEmailMessage = `----
From: Michael Pivato Contact Form <contact@michaelpivato.dev>
To: Michael Pivato <contact@michaelpivato.dev
@@ -53,7 +37,7 @@ ${message}
);
try {
await env.SEB.send(cfMessage);
await (context.env as any).SEB.send(cfMessage);
} catch (e) {
return new Response(e.message);
}
@@ -67,6 +51,5 @@ ${message}
return Response.redirect("https://michaelpivato.dev");
},
});
const formatEmptyString = (s: string) => s ?? "Not Specified";
})(context);
};