mirror of
https://github.com/Ravinou/borgwarehouse
synced 2026-03-14 22:35:46 +01:00
refactor: ⚡ sendTestEmail API
This commit is contained in:
parent
b266787295
commit
acdaaffc16
2 changed files with 28 additions and 31 deletions
|
|
@ -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,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
28
pages/api/account/sendTestEmail.ts
Normal file
28
pages/api/account/sendTestEmail.ts
Normal 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}` });
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue