Wrap static forms plugin to retrieve email in context
All checks were successful
release / Publish to Cloudflare Pages (push) Successful in 1m20s
All checks were successful
release / Publish to Cloudflare Pages (push) Successful in 1m20s
This commit is contained in:
@@ -1,37 +1,21 @@
|
|||||||
import staticFormsPlugin from "@cloudflare/pages-plugin-static-forms";
|
import staticFormsPlugin from "@cloudflare/pages-plugin-static-forms";
|
||||||
import { EmailMessage } from "cloudflare:email";
|
import { EmailMessage } from "cloudflare:email";
|
||||||
|
|
||||||
export const onRequest: PagesFunction = staticFormsPlugin({
|
const formatEmptyString = (s: string) => s ?? "Not Specified";
|
||||||
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.
|
export const onRequest: PagesFunction = (context) => {
|
||||||
if ((fullName || email) && message) {
|
// Wrap static forms plugin so we can extract the env to use email routing
|
||||||
// const emailMessage = createMimeMessage();
|
return staticFormsPlugin({
|
||||||
// emailMessage.setSender({
|
respondWith: async ({ formData }) => {
|
||||||
// name: "Michael Pivato Contact Form",
|
const fullName = formData.get("name");
|
||||||
// addr: "contact@michaelpivato.dev",
|
const organisation = formData.get("org") ?? "Unknown Organisation";
|
||||||
// });
|
const email = formData.get("email");
|
||||||
// emailMessage.setRecipient("contact@michaelpivato.dev");
|
const mobile = formData.get("mobile") ?? "Unknown Mobile";
|
||||||
// emailMessage.setSubject(`Message from ${fullName ?? email}`);
|
const message = formData.get("message");
|
||||||
// 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:
|
// Must have some kind of identifiable information for me to actually care about them.
|
||||||
// ${message}
|
if ((fullName || email) && message) {
|
||||||
// `,
|
const rawEmailMessage = `----
|
||||||
// });
|
|
||||||
|
|
||||||
const rawEmailMessage = `----
|
|
||||||
From: Michael Pivato Contact Form <contact@michaelpivato.dev>
|
From: Michael Pivato Contact Form <contact@michaelpivato.dev>
|
||||||
To: Michael Pivato <contact@michaelpivato.dev
|
To: Michael Pivato <contact@michaelpivato.dev
|
||||||
Subject: Message from ${fullName ?? email}
|
Subject: Message from ${fullName ?? email}
|
||||||
@@ -46,27 +30,26 @@ Message:
|
|||||||
${message}
|
${message}
|
||||||
----`;
|
----`;
|
||||||
|
|
||||||
const cfMessage = new EmailMessage(
|
const cfMessage = new EmailMessage(
|
||||||
"contact@michaelpivato.dev",
|
"contact@michaelpivato.dev",
|
||||||
"contact@michaelpivato.dev",
|
"contact@michaelpivato.dev",
|
||||||
rawEmailMessage
|
rawEmailMessage
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await env.SEB.send(cfMessage);
|
await (context.env as any).SEB.send(cfMessage);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return new Response(e.message);
|
return new Response(e.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("Full Name: " + fullName ?? "fullname");
|
||||||
|
console.log("Organisation: " + organisation);
|
||||||
|
console.log("Email: " + email ?? "email");
|
||||||
|
console.log("Mobile: " + mobile ?? "mobile");
|
||||||
|
console.log("Message: " + message);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("Full Name: " + fullName ?? "fullname");
|
return Response.redirect("https://michaelpivato.dev");
|
||||||
console.log("Organisation: " + organisation);
|
},
|
||||||
console.log("Email: " + email ?? "email");
|
})(context);
|
||||||
console.log("Mobile: " + mobile ?? "mobile");
|
};
|
||||||
console.log("Message: " + message);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Response.redirect("https://michaelpivato.dev");
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const formatEmptyString = (s: string) => s ?? "Not Specified";
|
|
||||||
|
|||||||
Reference in New Issue
Block a user