Files
website/contact-email-worker/src/index.ts
vato007 7788da14e5
All checks were successful
release / Publish to Cloudflare Pages (push) Successful in 1m37s
Remove redundant try catch
2025-04-23 14:58:12 +09:30

53 lines
1.3 KiB
TypeScript

import { EmailMessage } from "cloudflare:email";
import { WorkerEntrypoint } from "cloudflare:workers";
import { createMimeMessage } from "mimetext";
const formatEmptyString = (s: string) => s ?? "Not Specified";
interface EmailDetails {
fullName: string;
organisation: string;
email: string;
mobile: string;
message: string;
}
export default class SendEmailWorker extends WorkerEntrypoint<Env> {
async fetch() {
return new Response("Unimplemented");
}
async sendEmail({
fullName,
organisation,
email,
mobile,
message,
}: EmailDetails) {
const msg = createMimeMessage();
msg.setSender({
name: "Michael Pivato Contact Form",
addr: "contact@michaelpivato.dev",
});
msg.setRecipient("contact@michaelpivato.dev");
msg.setSubject(`Message from ${fullName ?? email}`);
msg.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 cfMessage = new EmailMessage(
"contact@michaelpivato.dev",
"contact@michaelpivato.dev",
msg.asRaw()
);
this.ctx.waitUntil(this.env.SEB.send(cfMessage));
}
}