Add Resend contact sync for contact form submissions

This commit is contained in:
nmemmert
2026-04-09 15:54:53 -04:00
parent 2653d95605
commit 3a9d11cf40
2 changed files with 49 additions and 0 deletions
+45
View File
@@ -13,6 +13,14 @@ function escapeHtml(value) {
.replace(/'/g, ''')
}
function splitName(fullName) {
const parts = fullName.trim().split(/\s+/).filter(Boolean)
return {
firstName: parts[0] ?? '',
lastName: parts.slice(1).join(' '),
}
}
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const DATA_DIR = path.join(__dirname, 'data')
@@ -113,6 +121,43 @@ app.post('/api/contact', contactRateLimit, async (req, res) => {
})
const resend = new Resend(process.env.RESEND_API_KEY)
const { firstName, lastName } = splitName(trimmedName)
try {
const { error: contactError } = await resend.contacts.create({
email: trimmedEmail,
firstName,
lastName,
unsubscribed: false,
...(process.env.RESEND_SEGMENT_ID
? { segments: [{ id: process.env.RESEND_SEGMENT_ID }] }
: {}),
properties: {
source: 'website-contact-form',
last_message_at: new Date().toISOString(),
},
})
if (contactError) {
const { error: updateError } = await resend.contacts.update({
email: trimmedEmail,
firstName,
lastName,
unsubscribed: false,
properties: {
source: 'website-contact-form',
last_message_at: new Date().toISOString(),
},
})
if (updateError) {
console.error('[contact] contact sync error:', updateError)
}
}
} catch (contactSyncErr) {
console.error('[contact] contact sync exception:', contactSyncErr)
}
const { error } = await resend.emails.send({
from: process.env.RESEND_FROM ?? 'Verse by Verse with Nate <onboarding@resend.dev>',
to: [process.env.RESEND_TO ?? 'vbvwithnate@outlook.com'],