Add contact form validation

This commit is contained in:
Paul Nicoué 2023-01-27 16:12:44 +01:00
parent 2a4cf52465
commit d3465c6ea3
12 changed files with 429 additions and 204 deletions

View file

@ -0,0 +1,43 @@
import Mailjet from 'node-mailjet';
export default defineEventHandler(async (event) => {
// Handle request
const body = await readBody(event);
// Instantiate and setup Mailjet
const mailjet = new Mailjet({
apiKey: useRuntimeConfig().mailjetApiPublic,
apiSecret: useRuntimeConfig().mailjetApiPrivate
});
// Send email
try {
const response = await mailjet
.post('send', { version: 'v3.1' })
.request({
Messages: [
{
From: {
Email: 'contact@paulnicoue.com',
Name: body.name,
},
To: [
{
Email: 'contact@paulnicoue.com',
Name: 'Paul Nicoué',
},
],
Subject: body.subject,
TextPart: body.message
}
]
});
console.log(response.body);
} catch (error) {
console.log(error.statusCode);
}
// End response without providing data
event.node.res.end();
});

View file

@ -1,65 +0,0 @@
import Mailjet from 'node-mailjet';
export default defineEventHandler(async (event) => {
const body = await readBody(event);
// const mailjet = new Mailjet({
// apiKey: useRuntimeConfig().mailjetApiPublic,
// apiSecret: useRuntimeConfig().mailjetApiPrivate
// });
// const request = mailjet
// .post('send', { version: 'v3.1' })
// .request({
// Messages: [
// {
// From: {
// Email: 'contact@paulnicoue.com',
// Name: body.name,
// },
// To: [
// {
// Email: 'contact@paulnicoue.com',
// Name: 'Paul Nicoué',
// },
// ],
// Subject: body.subject,
// TextPart: body.message
// }
// ]
// });
// request
// .then(result => {console.log(result.body)})
// .catch(err => {console.log(err.statusCode)});
Mailjet
.apiConnect(
useRuntimeConfig().mailjetApiPublic,
useRuntimeConfig().mailjetApiPrivate
)
.post('send', { version: 'v3.1' })
.request({
Messages: [
{
From: {
Email: 'contact@paulnicoue.com',
Name: body.name,
},
To: [
{
Email: 'contact@paulnicoue.com',
Name: 'Paul Nicoué',
},
],
Subject: body.subject,
TextPart: body.message
}
]
})
.then((result) => console.log(result.body))
.catch((err) => console.log(err.statusCode));
return event.node.res.end();
});