generated from theodorusclarence/ts-nextjs-tailwind-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: re-add contacts/send api endpoint
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as sgMail from '@sendgrid/mail'; | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse, | ||
) { | ||
const { name, company, email, subject, message } = req.body; | ||
|
||
if (!email) { | ||
return res.status(400).json({ error: 'Email address is required' }); | ||
} | ||
|
||
const sgApiKey = process.env.SENDGRID_API_KEY || ''; | ||
|
||
sgMail.setApiKey(sgApiKey); | ||
|
||
const msg = { | ||
to: 'info@martacodes.it', | ||
cc: 'marta_panc@me.com', | ||
from: `${name} ${company ? ' @ ' + company : ''} <marta_panc@me.com>`, | ||
replyTo: email, | ||
subject: subject, | ||
text: message, | ||
html: `<div><p>${message}</p></div>`, | ||
}; | ||
|
||
try { | ||
await sgMail.send(msg); | ||
return res.status(200).json({ message: 'Email sent' }); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error('Error sending email:', error); | ||
return res.status(500).json({ error: 'Email could not be sent' }); | ||
} | ||
} |