From 4fce2ddaaba869ecf85d0718f7ccef0063fa6687 Mon Sep 17 00:00:00 2001 From: bsourisse Date: Thu, 19 Jan 2023 12:54:48 +0100 Subject: [PATCH] feat: API to send test email --- package-lock.json | 14 ++++++++ package.json | 1 + pages/api/account/sendEmail.js | 60 ++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 pages/api/account/sendEmail.js diff --git a/package-lock.json b/package-lock.json index 5baafad..fedc654 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "chart.js": "^3.9.1", "next": "^13.0.5", "next-auth": "^4.17.0", + "nodemailer": "^6.9.0", "react": "^18.2.0", "react-chartjs-2": "^4.3.1", "react-dom": "^18.2.0", @@ -3446,6 +3447,14 @@ "integrity": "sha512-dFSmB8fFHEH/s81Xi+Y/15DQY6VHW81nXRj86EMSL3lmuTmK1e+aT4wrFCkTbm+gSwkw4KpX+rT/pMM2c1mF+A==", "peer": true }, + "node_modules/nodemailer": { + "version": "6.9.0", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.9.0.tgz", + "integrity": "sha512-jFaCEGTeT3E/m/5R2MHWiyQH3pSARECRUDM+1hokOYc3lQAAG7ASuy+2jIsYVf+RVa9zePopSQwKNVFH8DKUpA==", + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/oauth": { "version": "0.9.15", "resolved": "https://registry.npmjs.org/oauth/-/oauth-0.9.15.tgz", @@ -7147,6 +7156,11 @@ "integrity": "sha512-dFSmB8fFHEH/s81Xi+Y/15DQY6VHW81nXRj86EMSL3lmuTmK1e+aT4wrFCkTbm+gSwkw4KpX+rT/pMM2c1mF+A==", "peer": true }, + "nodemailer": { + "version": "6.9.0", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.9.0.tgz", + "integrity": "sha512-jFaCEGTeT3E/m/5R2MHWiyQH3pSARECRUDM+1hokOYc3lQAAG7ASuy+2jIsYVf+RVa9zePopSQwKNVFH8DKUpA==" + }, "oauth": { "version": "0.9.15", "resolved": "https://registry.npmjs.org/oauth/-/oauth-0.9.15.tgz", diff --git a/package.json b/package.json index ade3b38..4df0354 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "chart.js": "^3.9.1", "next": "^13.0.5", "next-auth": "^4.17.0", + "nodemailer": "^6.9.0", "react": "^18.2.0", "react-chartjs-2": "^4.3.1", "react-dom": "^18.2.0", diff --git a/pages/api/account/sendEmail.js b/pages/api/account/sendEmail.js new file mode 100644 index 0000000..ea1a8fe --- /dev/null +++ b/pages/api/account/sendEmail.js @@ -0,0 +1,60 @@ +//Lib +import { promises as fs } from 'fs'; +import path from 'path'; +import { authOptions } from '../auth/[...nextauth]'; +import { unstable_getServerSession } from 'next-auth/next'; + +export default async function handler(req, res) { + if (req.method == 'POST') { + //Verify that the user is logged in. + const session = await unstable_getServerSession(req, res, authOptions); + if (!session) { + res.status(401).json({ message: 'You must be logged in.' }); + return; + } + + try { + let nodemailer = require('nodemailer'); + 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: true, + }); + + const mailData = { + from: process.env.MAIL_SMTP_LOGIN, + to: 'email@test.fr', + subject: `Mail test`, + text: 'Corps de test', + html: `
HTML test

Sent from: test

`, + }; + + 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; + } + }); + } catch (err) { + console.log(err); + res.status(500).json({ + status: 500, + message: 'API error, contact the administrator.', + }); + } + } +}