personal-site/pages/api/contactForm.ts

55 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-10-24 17:59:00 +00:00
/**
* Imports
*/
const fs = require("fs");
const sanitizeHtml = require("sanitize-html");
const nodemailer = require("nodemailer");
import sanitizeHtmlOptions from "../../functions/backend/sanitizeHtmlOptions";
import { NextApiRequest, NextApiResponse } from "next";
let transporter = nodemailer.createTransport({
host: process.env.TBENMAIL_HOST,
2024-01-30 03:27:23 +00:00
port: 465,
2023-10-24 17:59:00 +00:00
auth: {
user: process.env.TBENMAIL_EMAIL,
pass: process.env.TBENMAIL_EMAIL_PASSWORD,
},
});
/**
* API handler
*
*/
2023-11-21 05:31:03 +00:00
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
2023-10-24 17:59:00 +00:00
if (req.method === "POST") {
let name = req.body.name;
let email = req.body.email;
let message = req.body.message;
const html = `<h1>Message from ${name} | ${email}</h1><h4>Name:</h4><p>${name}</p><h4>Email:</h4><p>${email}</p><h4>Message:</h4><p>${message}</p>`;
const sanitizedHtml = sanitizeHtml(html, sanitizeHtmlOptions);
try {
let info = await transporter.sendMail({
from: `"Tben.me" <${process.env.TBENMAIL_EMAIL}>`,
2023-11-21 05:31:03 +00:00
to: "benoti.san@gmail.com,benoti.sanchez@gmail.com,benoti.san@proton.me",
2023-10-24 17:59:00 +00:00
subject: "Tben.me | Client Message",
text: "Hello from tben",
html: sanitizedHtml,
});
console.log("Message sent: %s", info.messageId);
res.json({ msg: "Success", info: info });
} catch (error) {
console.log(error);
2024-01-30 03:27:23 +00:00
res.json({
msg: "Please try again in a minute.",
});
2023-10-24 17:59:00 +00:00
}
}
}