Rework email sending to be RFC5322 compliant by using mimetext package
Some checks failed
release / Publish to Cloudflare Pages (push) Failing after 58s

This commit is contained in:
2025-02-03 17:53:57 +10:30
parent b543b57b4e
commit 23f0d518c5
4 changed files with 248 additions and 52 deletions

View File

@@ -1,43 +1,41 @@
import staticFormsPlugin from "@cloudflare/pages-plugin-static-forms";
interface EmailDetails {
fullName: string;
organisation: string;
email: string;
mobile: string;
message: string;
}
interface SendEmailWorker {
sendEmail(rawMessage: string): Promise<Response>;
sendEmail(rawMessage: EmailDetails): 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 organisation = formData.get("org");
const email = formData.get("email");
const mobile = formData.get("mobile") ?? "Unknown Mobile";
const mobile = formData.get("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);
await context.env.SERVICE.sendEmail({
fullName,
organisation,
email,
mobile,
message,
});
} catch (e) {
return new Response(e);
}