Skip to content

Commit

Permalink
feat: re-add contacts/send api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
martapanc committed Feb 16, 2024
1 parent 3baf6d6 commit e75d827
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/pages/api/contacts/send.ts
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' });
}
}

0 comments on commit e75d827

Please sign in to comment.