refactor: sendTestEmail API

This commit is contained in:
Ravinou 2025-02-23 10:41:02 +01:00
commit acdaaffc16
No known key found for this signature in database
GPG key ID: EEEE670C40F6A4D7
2 changed files with 28 additions and 31 deletions

View file

@ -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,
});
}
}
}

View file

@ -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}` });
}
}