From 5413c7dddf6270ec15d6c5259237edf8a8c48c8d Mon Sep 17 00:00:00 2001 From: rui hildt Date: Sat, 29 Aug 2020 14:30:27 +0200 Subject: [PATCH] Add SMTP status check --- api/server.js | 9 +++++++-- config/config.js | 7 ++++--- helpers/smtpStatus.js | 15 +++++++++++++++ helpers/smtpTransporter.js | 7 ++++++- 4 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 helpers/smtpStatus.js diff --git a/api/server.js b/api/server.js index 81c8a08..24d8dc6 100644 --- a/api/server.js +++ b/api/server.js @@ -4,6 +4,8 @@ const bodyParser = require('body-parser'); const cors = require('cors'); const helmet = require('helmet'); +const smtpStatus = require('../helpers/smtpStatus'); + const accountsRoute = require('./routes/accountRoute'); const meetingsRoute = require('./routes/meetingRoute'); const participantsRoute = require('./routes/participantRoute'); @@ -34,10 +36,13 @@ server.use('/api/possible-dates', possibleDatesRoute); server.use('/api/availability', availabilityRoute); server.use('/api/auth', authRoute); -server.get("/", (req, res) => { +const smtpMessage = smtpStatus(); + +server.get('/', (req, res) => { res.json({ message: `Meeting Planner Backend API :)`, - documentation: `Check out: https://git.armada.digital/meeting-planner/backend` + documentation: `Check out: https://git.armada.digital/meeting-planner/backend`, + SMTP: `${smtpMessage}`, }); }); diff --git a/config/config.js b/config/config.js index bd926d9..a42fed1 100644 --- a/config/config.js +++ b/config/config.js @@ -7,12 +7,13 @@ module.exports = { dbURL: process.env.DATABASE_URL, // JWT jwtSecret: process.env.JWT_SECRET, - saltingRounds: process.env.SALTING_ROUNDS || 10, + saltingRounds: 10, // NODEMAILER smtpHost: process.env.SMTP_HOST, smtpPort: process.env.SMTP_PORT, smtpUsername: process.env.SMTP_USERNAME, smtpPassword: process.env.SMTP_PASSWORD, - smtpSecure: process.env.SMTP_SECURE || true, // use TLS - smtpPool: process.env.SMTP_POOL|| true, // Pool connections to server + smtpRequireTLS: true, // True to force use of StartTLS + smtpSecure: false, // False to force use of StartTLS + smtpPool: true, // Pool connections to server }; diff --git a/helpers/smtpStatus.js b/helpers/smtpStatus.js new file mode 100644 index 0000000..722ccea --- /dev/null +++ b/helpers/smtpStatus.js @@ -0,0 +1,15 @@ +const smtpTransporter = require('./smtpTransporter'); + +function smtpStatus() { + smtpTransporter.verify(function (error, success) { + if (error) { + console.log(error); + return `SMTP is not working, check your configuration.` + } else { + console.log('Server is ready to take our messages'); + return `SMTP is correctly configured.` + } + }); +} + +module.exports = smtpStatus; diff --git a/helpers/smtpTransporter.js b/helpers/smtpTransporter.js index 672b0c8..cbd20df 100644 --- a/helpers/smtpTransporter.js +++ b/helpers/smtpTransporter.js @@ -6,15 +6,20 @@ const { smtpPort, smtpUsername, smtpPassword, + smtpSecure, + smtpRequireTLS, } = require('../config/config'); -nodemailer.createTransport({ +const smtpTransporter = nodemailer.createTransport({ pool: smtpPool, host: smtpHost, port: smtpPort, + requireTLS: smtpRequireTLS, secure: smtpSecure, auth: { user: smtpUsername, pass: smtpPassword, }, }); + +module.exports = smtpTransporter;