Add a contact form to the resume #1
@@ -1,37 +1,21 @@
|
||||
import staticFormsPlugin from "@cloudflare/pages-plugin-static-forms";
|
||||
import { EmailMessage } from "cloudflare:email";
|
||||
|
||||
export const onRequest: PagesFunction = 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");
|
||||
const formatEmptyString = (s: string) => s ?? "Not Specified";
|
||||
|
||||
// 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)}
|
||||
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";
|
||||
const email = formData.get("email");
|
||||
const mobile = formData.get("mobile") ?? "Unknown Mobile";
|
||||
const message = formData.get("message");
|
||||
|
||||
// Message:
|
||||
// ${message}
|
||||
// `,
|
||||
// });
|
||||
|
||||
const rawEmailMessage = `----
|
||||
// Must have some kind of identifiable information for me to actually care about them.
|
||||
if ((fullName || email) && message) {
|
||||
const rawEmailMessage = `----
|
||||
From: Michael Pivato Contact Form <contact@michaelpivato.dev>
|
||||
To: Michael Pivato <contact@michaelpivato.dev
|
||||
Subject: Message from ${fullName ?? email}
|
||||
@@ -46,27 +30,26 @@ Message:
|
||||
${message}
|
||||
----`;
|
||||
|
||||
const cfMessage = new EmailMessage(
|
||||
"contact@michaelpivato.dev",
|
||||
"contact@michaelpivato.dev",
|
||||
rawEmailMessage
|
||||
);
|
||||
const cfMessage = new EmailMessage(
|
||||
"contact@michaelpivato.dev",
|
||||
"contact@michaelpivato.dev",
|
||||
rawEmailMessage
|
||||
);
|
||||
|
||||
try {
|
||||
await env.SEB.send(cfMessage);
|
||||
} catch (e) {
|
||||
return new Response(e.message);
|
||||
try {
|
||||
await (context.env as any).SEB.send(cfMessage);
|
||||
} catch (e) {
|
||||
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");
|
||||
console.log("Organisation: " + organisation);
|
||||
console.log("Email: " + email ?? "email");
|
||||
console.log("Mobile: " + mobile ?? "mobile");
|
||||
console.log("Message: " + message);
|
||||
}
|
||||
|
||||
return Response.redirect("https://michaelpivato.dev");
|
||||
},
|
||||
});
|
||||
|
||||
const formatEmptyString = (s: string) => s ?? "Not Specified";
|
||||
return Response.redirect("https://michaelpivato.dev");
|
||||
},
|
||||
})(context);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user