All checks were successful
release / Publish to Cloudflare Pages (push) Successful in 1m4s
50 lines
1.7 KiB
TypeScript
50 lines
1.7 KiB
TypeScript
import staticFormsPlugin from "@cloudflare/pages-plugin-static-forms";
|
|
|
|
interface SendEmailWorker {
|
|
sendEmail(rawMessage: string): Promise<Response>;
|
|
}
|
|
|
|
interface Env {
|
|
SERVICE: SendEmailWorker;
|
|
}
|
|
|
|
const formatEmptyString = (s: string) => s ?? "Not Specified";
|
|
|
|
export const onRequest: PagesFunction<Env> = (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";
|
|
const email = formData.get("email");
|
|
const mobile = formData.get("mobile") ?? "Unknown Mobile";
|
|
const message = formData.get("message");
|
|
|
|
// Must have some kind of identifiable information for me to actually care about them.
|
|
if ((fullName || email) && message) {
|
|
try {
|
|
const rawEmailMessage = `----
|
|
From: Michael Pivato Contact Form <contact@michaelpivato.dev>
|
|
To: Michael Pivato <contact@michaelpivato.dev>
|
|
Subject: Message from ${fullName ?? email}
|
|
|
|
You've received a new message from ${fullName ?? email}.
|
|
Full Name: ${formatEmptyString(fullName)}
|
|
Organisation: ${formatEmptyString(organisation)}
|
|
Email: ${formatEmptyString(email)}
|
|
Mobile: ${formatEmptyString(mobile)}
|
|
|
|
Message:
|
|
${message}
|
|
----`;
|
|
await context.env.SERVICE.sendEmail(rawEmailMessage);
|
|
} catch (e) {
|
|
return new Response(e);
|
|
}
|
|
}
|
|
|
|
return Response.redirect("https://michaelpivato.dev");
|
|
},
|
|
})(context);
|
|
};
|