diff --git a/helpers/functions/nodemailerSMTP.js b/helpers/functions/nodemailerSMTP.js new file mode 100644 index 0000000..fbb924e --- /dev/null +++ b/helpers/functions/nodemailerSMTP.js @@ -0,0 +1,19 @@ +//Lib +import nodemailer from 'nodemailer'; + +export default function nodemailerSMTP() { + const transporter = nodemailer.createTransport({ + port: process.env.MAIL_SMTP_PORT, + host: process.env.MAIL_SMTP_HOST, + auth: { + user: process.env.MAIL_SMTP_LOGIN, + pass: process.env.MAIL_SMTP_PWD, + }, + secure: process.env.MAIL_SMTP_SECURE, //Use TLS or not + tls: { + // do not fail on invalid certs >> allow self-signed or invalid TLS certificate + rejectUnauthorized: process.env.MAIL_REJECT_SELFSIGNED_TLS, + }, + }); + return transporter; +} diff --git a/helpers/templates/emailTest.js b/helpers/templates/emailTest.js new file mode 100644 index 0000000..2309714 --- /dev/null +++ b/helpers/templates/emailTest.js @@ -0,0 +1,82 @@ +export default function emailTest(mailTo, username) { + const template = { + from: 'BorgWarehouse' + '<' + process.env.MAIL_SMTP_FROM + '>', + to: mailTo, + subject: 'Testing email settings', + text: 'Corps de test', + html: + ` +
+
+ +
+
+ BorgWarehouse +
+
+ + + + + +
+ +

Good job, ` + + username + + ` !

+ +

If you received this email then the email address configuration seems to be correct.

+ + +
+

About BorgWarehouse

+
+ + `, + }; + return template; +} diff --git a/pages/api/account/sendTestEmail.js b/pages/api/account/sendTestEmail.js index 4bffd46..417f197 100644 --- a/pages/api/account/sendTestEmail.js +++ b/pages/api/account/sendTestEmail.js @@ -1,7 +1,8 @@ //Lib import { authOptions } from '../auth/[...nextauth]'; import { unstable_getServerSession } from 'next-auth/next'; -import nodemailer from 'nodemailer'; +import nodemailerSMTP from '../../../helpers/functions/nodemailerSMTP'; +import emailTest from '../../../helpers/templates/emailTest'; export default async function handler(req, res) { if (req.method == 'POST') { @@ -13,99 +14,10 @@ export default async function handler(req, res) { } //Create the SMTP Transporter - const transporter = nodemailer.createTransport({ - port: process.env.MAIL_SMTP_PORT, - host: process.env.MAIL_SMTP_HOST, - auth: { - user: process.env.MAIL_SMTP_LOGIN, - pass: process.env.MAIL_SMTP_PWD, - }, - secure: process.env.MAIL_SMTP_SECURE, //Use TLS or not - tls: { - // do not fail on invalid certs >> allow self-signed or invalid TLS certificate - rejectUnauthorized: process.env.MAIL_REJECT_SELFSIGNED_TLS, - }, - }); + const transporter = nodemailerSMTP(); + //Mail options - const mailData = { - from: 'BorgWarehouse' + '<' + process.env.MAIL_SMTP_FROM + '>', - to: session.user.email, - subject: 'Testing email settings', - text: 'Corps de test', - html: - ` -
-
- -
-
- BorgWarehouse -
-
- - - - - -
- -

Good job, ` + - session.user.name + - ` !

- -

If you received this email then the email address configuration seems to be correct.

- - -
-

About BorgWarehouse

-
- - `, - }; + const mailData = emailTest(session.user.email, session.user.name); //Send mail try {