From 683ff54cdbb6df922a31081d8adb849f8c5406f3 Mon Sep 17 00:00:00 2001 From: Ajay Ghanwat Date: Mon, 13 Apr 2026 12:59:07 +0530 Subject: [PATCH] feat: add user confirmation email and admin BCC for contact form submissions Co-Authored-By: Claude Opus 4.6 --- src/pages/api/contact.ts | 63 ++++++++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 9 deletions(-) diff --git a/src/pages/api/contact.ts b/src/pages/api/contact.ts index 2e9ff86..1137a35 100644 --- a/src/pages/api/contact.ts +++ b/src/pages/api/contact.ts @@ -194,9 +194,11 @@ async function sendContactEmail(data: ContactFormData): Promise { const fromAddress = smtpFrom || smtpUser; - const info = await smtpTransporter.sendMail({ + // 1) Admin notification email with BCC to SMTP_USER for delivery confirmation + const adminInfo = await smtpTransporter.sendMail({ from: `"CompanySite Contact Form" <${fromAddress}>`, to: recipientEmail, + bcc: process.env.SMTP_USER, // BCC to admin inbox as delivery confirmation replyTo: data.email, subject: `[Contact Form] ${subjectLabels[data.subject] ?? data.subject} - ${data.name}`, text: [ @@ -226,7 +228,46 @@ async function sendContactEmail(data: ContactFormData): Promise { `, }); - return info; + // 2) User confirmation email — proves delivery works AND provides good UX + const userConfirmInfo = await smtpTransporter.sendMail({ + from: `"CompanySite" <${fromAddress}>`, + to: data.email, + subject: 'We received your message - CompanySite', + text: [ + `Hi ${data.name},`, + ``, + `Thank you for reaching out to us! We have received your message regarding "${subjectLabels[data.subject] ?? data.subject}".`, + ``, + `Our team will review your inquiry and get back to you as soon as possible, typically within 1-2 business days.`, + ``, + `For your reference, here is a summary of your submission:`, + ` Subject: ${subjectLabels[data.subject] ?? data.subject}`, + ` Date: ${new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' })}`, + ``, + `If you have any urgent questions, feel free to reply to this email.`, + ``, + `Best regards,`, + `The CompanySite Team`, + ].join('\n'), + html: ` +
+

Thank you for contacting us!

+

Hi ${escapeHtml(data.name)},

+

We have received your message regarding "${escapeHtml(subjectLabels[data.subject] ?? data.subject)}".

+

Our team will review your inquiry and get back to you as soon as possible, typically within 1-2 business days.

+
+

Subject: ${escapeHtml(subjectLabels[data.subject] ?? data.subject)}

+

Date: ${new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' })}

+
+

If you have any urgent questions, feel free to reply to this email.

+

Best regards,
The CompanySite Team

+
+

This is an automated confirmation. Please do not reply unless you need further assistance.

+
+ `, + }); + + return { adminInfo, userConfirmInfo }; } catch (err) { // If nodemailer isn't installed, treat as graceful degradation (log only) if (err instanceof Error && err.message.includes('Cannot find module')) { @@ -374,14 +415,18 @@ export const POST: APIRoute = async ({ request, clientAddress }) => { console.log('Sending contact email to: ' + recipientEmail); sendContactEmail(formData) - .then((info) => { - console.log('Contact email delivered successfully to:', recipientEmail, { - messageId: info?.messageId, + .then((result) => { + console.log('Contact emails delivered successfully:', { + adminRecipient: recipientEmail, + adminMessageId: result?.adminInfo?.messageId, + userRecipient: formData.email, + userMessageId: result?.userConfirmInfo?.messageId, }); - logger.info('api.contact', 'Contact email delivered successfully', { - recipient: recipientEmail, - messageId: info?.messageId, - from: formData.email, + logger.info('api.contact', 'Contact emails delivered successfully', { + adminRecipient: recipientEmail, + adminMessageId: result?.adminInfo?.messageId, + userRecipient: formData.email, + userMessageId: result?.userConfirmInfo?.messageId, subject: formData.subject, }); })