feat: add user confirmation email and admin BCC for contact form submissions

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Ajay Ghanwat
2026-04-13 12:59:07 +05:30
co-authored by Claude Opus 4.6
parent da0e2367c0
commit 683ff54cdb
+54 -9
View File
@@ -194,9 +194,11 @@ async function sendContactEmail(data: ContactFormData): Promise<void> {
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<void> {
`,
});
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: `
<div style="font-family:Arial,sans-serif;max-width:600px;margin:0 auto;padding:20px">
<h2 style="color:#333">Thank you for contacting us!</h2>
<p>Hi ${escapeHtml(data.name)},</p>
<p>We have received your message regarding <strong>"${escapeHtml(subjectLabels[data.subject] ?? data.subject)}"</strong>.</p>
<p>Our team will review your inquiry and get back to you as soon as possible, typically within <strong>1-2 business days</strong>.</p>
<div style="background:#f9f9f9;border-left:4px solid #007bff;padding:12px 16px;margin:20px 0">
<p style="margin:4px 0"><strong>Subject:</strong> ${escapeHtml(subjectLabels[data.subject] ?? data.subject)}</p>
<p style="margin:4px 0"><strong>Date:</strong> ${new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' })}</p>
</div>
<p>If you have any urgent questions, feel free to reply to this email.</p>
<p style="margin-top:24px">Best regards,<br><strong>The CompanySite Team</strong></p>
<hr style="border:none;border-top:1px solid #eee;margin-top:24px">
<p style="color:#888;font-size:12px">This is an automated confirmation. Please do not reply unless you need further assistance.</p>
</div>
`,
});
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,
});
})