diff --git a/app/api/contact/route.ts b/app/api/contact/route.ts index 4d82a3a..30825bc 100644 --- a/app/api/contact/route.ts +++ b/app/api/contact/route.ts @@ -6,7 +6,7 @@ export const dynamic = 'force-dynamic' export async function POST(request: NextRequest) { try { - const { name, email, message } = await request.json() + const { name, email, message, recaptchaToken } = await request.json() // Verify reCAPTCHA first const recaptchaResponse = await fetch('https://www.google.com/recaptcha/api/siteverify', { @@ -38,7 +38,9 @@ export async function POST(request: NextRequest) {

Message:

${message}

` - }) + } + + await sendEmail(msg) return NextResponse.json({ success: true }) } catch (error) { diff --git a/lib/sendgrid.ts b/lib/sendgrid.ts index 7ec38b5..792a9aa 100644 --- a/lib/sendgrid.ts +++ b/lib/sendgrid.ts @@ -1,13 +1,19 @@ import sgMail from '@sendgrid/mail' -if (!process.env.SENDGRID_API_KEY) { - throw new Error('SENDGRID_API_KEY is not set') -} +let apiKeyConfigured = false -sgMail.setApiKey(process.env.SENDGRID_API_KEY) +const configureApiKey = () => { + if (apiKeyConfigured) return + if (!process.env.SENDGRID_API_KEY) { + throw new Error('SENDGRID_API_KEY is not set') + } + sgMail.setApiKey(process.env.SENDGRID_API_KEY) + apiKeyConfigured = true +} export const sendEmail = async (options: sgMail.MailDataRequired) => { try { + configureApiKey() await sgMail.send(options) return { success: true } } catch (error: any) {