diff --git a/src/utils/email-handler.ts b/src/utils/email-handler.ts
index 7137ca8..41c6756 100644
--- a/src/utils/email-handler.ts
+++ b/src/utils/email-handler.ts
@@ -171,6 +171,16 @@ export async function sendEmail(to: string, subject: string, html: string, text:
}
}
+// Utility to escape HTML special characters
+function escapeHtml(str: string): string {
+ return str
+ .replace(/&/g, '&')
+ .replace(//g, '>')
+ .replace(/"/g, '"')
+ .replace(/'/g, ''');
+}
+
// Send admin notification email
export async function sendAdminNotification(
name: string,
@@ -195,7 +205,7 @@ export async function sendAdminNotification(
return false;
}
- const subject = `New Contact Form Submission from ${name}`;
+ const subject = `New Contact Form Submission from ${escapeHtml(name)}`;
const html = `
@@ -271,19 +281,19 @@ export async function sendAdminNotification(
Name
-
${name}
+
${escapeHtml(name)}
Email
-
${email}
+
${escapeHtml(email)}
Message
-
${message.replace(/\n/g, '
')}
+
${escapeHtml(message).replace(/\n/g, '
')}
-
Dear ${name},
+
Dear ${escapeHtml(name)},
Thank you for contacting ${WEBSITE_NAME}. We have received your message and will get back to you as soon as possible.
Your Message:
-
${message.replace(/\n/g, '
')}
+
${escapeHtml(message).replace(/\n/g, '
')}
If you have any additional information to share, please don't hesitate to reply to this email.