Skip to content

Commit

Permalink
fix: 🐛 allow self-signed cert and no-auth for smtp settings #367 #364
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravinou committed Dec 26, 2024
1 parent 0d645fc commit 88a80f4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 31 deletions.
12 changes: 9 additions & 3 deletions Containers/UserSettings/EmailAlertSettings/EmailAlertSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -124,7 +131,6 @@ export default function EmailAlertSettings() {
})
.catch((error) => {
setTestIsLoading(false);
console.log(error);
setError('Send email failed. Contact your administrator.');
setTimeout(() => {
setError();
Expand Down
10 changes: 5 additions & 5 deletions helpers/functions/nodemailerSMTP.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 7 additions & 23 deletions pages/api/account/sendTestEmail.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,30 @@
//Lib
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') {
//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,
});
}
}
Expand Down

0 comments on commit 88a80f4

Please sign in to comment.