From 88a80f42e8c874f94ef61e5133eaf13d2a318032 Mon Sep 17 00:00:00 2001 From: Ravinou Date: Thu, 26 Dec 2024 11:35:37 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20allow=20self-signed=20cer?= =?UTF-8?q?t=20and=20no-auth=20for=20smtp=20settings=20#367=20#364?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EmailAlertSettings/EmailAlertSettings.js | 12 ++++++-- helpers/functions/nodemailerSMTP.js | 10 +++---- pages/api/account/sendTestEmail.js | 30 +++++-------------- 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/Containers/UserSettings/EmailAlertSettings/EmailAlertSettings.js b/Containers/UserSettings/EmailAlertSettings/EmailAlertSettings.js index 5e51e6db..27df8358 100644 --- a/Containers/UserSettings/EmailAlertSettings/EmailAlertSettings.js +++ b/Containers/UserSettings/EmailAlertSettings/EmailAlertSettings.js @@ -110,10 +110,17 @@ export default function EmailAlertSettings() { .then((response) => { if (!response.ok) { setTestIsLoading(false); - setError('Failed to send the notification.'); + response + .json() + .then((data) => { + setError(data.message || 'Failed to send the notification.'); + }) + .catch(() => { + setError('Failed to send the notification.'); + }); setTimeout(() => { setError(); - }, 4000); + }, 10000); } else { setTestIsLoading(false); setInfo(true); @@ -124,7 +131,6 @@ export default function EmailAlertSettings() { }) .catch((error) => { setTestIsLoading(false); - console.log(error); setError('Send email failed. Contact your administrator.'); setTimeout(() => { setError(); diff --git a/helpers/functions/nodemailerSMTP.js b/helpers/functions/nodemailerSMTP.js index 9ebe29dc..683951aa 100644 --- a/helpers/functions/nodemailerSMTP.js +++ b/helpers/functions/nodemailerSMTP.js @@ -3,15 +3,15 @@ import nodemailer from 'nodemailer'; export default function nodemailerSMTP() { const transporter = nodemailer.createTransport({ - port: process.env.MAIL_SMTP_PORT, + port: parseInt(process.env.MAIL_SMTP_PORT, 10), host: process.env.MAIL_SMTP_HOST, auth: { - user: process.env.MAIL_SMTP_LOGIN, - pass: process.env.MAIL_SMTP_PWD, + user: process.env.MAIL_SMTP_LOGIN || '', + pass: process.env.MAIL_SMTP_PWD || '', }, tls: { - // do not fail on invalid certs >> allow self-signed or invalid TLS certificate - rejectUnauthorized: process.env.MAIL_REJECT_SELFSIGNED_TLS, + // false value allow self-signed or invalid TLS certificate + rejectUnauthorized: process.env.MAIL_REJECT_SELFSIGNED_TLS === 'false' ? false : true, }, }); return transporter; diff --git a/pages/api/account/sendTestEmail.js b/pages/api/account/sendTestEmail.js index ea852048..9d3cdc46 100644 --- a/pages/api/account/sendTestEmail.js +++ b/pages/api/account/sendTestEmail.js @@ -1,4 +1,3 @@ -//Lib import { authOptions } from '../auth/[...nextauth]'; import { getServerSession } from 'next-auth/next'; import nodemailerSMTP from '../../../helpers/functions/nodemailerSMTP'; @@ -6,41 +5,26 @@ import emailTest from '../../../helpers/templates/emailTest'; export default async function handler(req, res) { if (req.method == 'POST') { - //Verify that the user is logged in. const session = await getServerSession(req, res, authOptions); if (!session) { - res.status(401).json({ message: 'You must be logged in.' }); - return; + 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); - //Send mail try { - transporter.sendMail(mailData, function (err, info) { - if (err) { - console.log(err); - res.status(400).json({ - message: 'An error occured while sending the email : ' + err, - }); - return; - } else { - console.log(info); - res.status(200).json({ - message: 'Mail successfully sent.', - }); - return; - } + const info = await transporter.sendMail(mailData); + console.log(info); + return res.status(200).json({ + message: 'Mail successfully sent', }); } catch (err) { console.log(err); - res.status(500).json({ - status: 500, - message: 'API error, contact the administrator.', + return res.status(400).json({ + message: 'An error occurred while sending the email: ' + err, }); } }