diff --git a/pages/api/account/sendTestEmail.js b/pages/api/account/sendTestEmail.js deleted file mode 100644 index 9d3cdc4..0000000 --- a/pages/api/account/sendTestEmail.js +++ /dev/null @@ -1,31 +0,0 @@ -import { authOptions } from '../auth/[...nextauth]'; -import { getServerSession } from 'next-auth/next'; -import nodemailerSMTP from '../../../helpers/functions/nodemailerSMTP'; -import emailTest from '../../../helpers/templates/emailTest'; - -export default async function handler(req, res) { - if (req.method == 'POST') { - const session = await getServerSession(req, res, authOptions); - if (!session) { - return res.status(401).json({ message: 'You must be logged in.' }); - } - - //Create the SMTP Transporter - const transporter = nodemailerSMTP(); - //Mail options - const mailData = emailTest(session.user.email, session.user.name); - - try { - const info = await transporter.sendMail(mailData); - console.log(info); - return res.status(200).json({ - message: 'Mail successfully sent', - }); - } catch (err) { - console.log(err); - return res.status(400).json({ - message: 'An error occurred while sending the email: ' + err, - }); - } - } -} diff --git a/pages/api/account/sendTestEmail.ts b/pages/api/account/sendTestEmail.ts new file mode 100644 index 0000000..3674277 --- /dev/null +++ b/pages/api/account/sendTestEmail.ts @@ -0,0 +1,28 @@ +import { authOptions } from '../auth/[...nextauth]'; +import { getServerSession } from 'next-auth/next'; +import { NextApiRequest, NextApiResponse } from 'next'; +import nodemailerSMTP from '~/helpers/functions/nodemailerSMTP'; +import emailTest from '~/helpers/templates/emailTest'; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + if (req.method !== 'POST') { + return res.status(405); + } + + const session = await getServerSession(req, res, authOptions); + if (!session) { + return res.status(401); + } + + try { + const transporter = nodemailerSMTP(); + const mailData = emailTest(session.user?.email, session.user?.name); + const info = await transporter.sendMail(mailData); + console.log(info); + + return res.status(200).json({ message: 'Mail successfully sent' }); + } catch (error) { + console.error(error); + return res.status(500).json({ message: `An error occurred while sending the email: ${error}` }); + } +}