Add a contact form to the resume (#1)
All checks were successful
release / Publish to Cloudflare Pages (push) Successful in 1m6s
All checks were successful
release / Publish to Cloudflare Pages (push) Successful in 1m6s
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
56
contact-email-worker/src/index.ts
Normal file
56
contact-email-worker/src/index.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
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}`,
|
||||
});
|
||||
try {
|
||||
const cfMessage = new EmailMessage(
|
||||
"contact@michaelpivato.dev",
|
||||
"contact@michaelpivato.dev",
|
||||
msg.asRaw()
|
||||
);
|
||||
await this.env.SEB.send(cfMessage);
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user