Rework email sending to be RFC5322 compliant by using mimetext package
Some checks failed
release / Publish to Cloudflare Pages (push) Failing after 58s
Some checks failed
release / Publish to Cloudflare Pages (push) Failing after 58s
This commit is contained in:
@@ -1,17 +1,52 @@
|
||||
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(rawMessage: string) {
|
||||
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",
|
||||
rawMessage
|
||||
msg.asRaw()
|
||||
);
|
||||
await this.env.SEB.send(cfMessage);
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user