diff --git a/Server/index.js b/Server/index.js index cd25e8399..306776643 100644 --- a/Server/index.js +++ b/Server/index.js @@ -15,78 +15,7 @@ const JobQueue = require("./service/jobQueue"); const pageSpeedCheckRouter = require("./routes/pageSpeedCheckRoute"); const nodemailer = require("nodemailer"); -const fs = require("fs"); -const path = require("path"); -const { compile } = require("handlebars"); -const { mjml2html } = require("mjml"); - -// Fetching Templates - -// Welcome Email Template -const welcomeEmailTemplatePath = path.join( - __dirname, - "./templates/welcomeEmail.mjml" -); -const welcomeEmailTemplateContent = fs.readFileSync( - welcomeEmailTemplatePath, - "utf8" -); -const welcomeEmailTemplate = compile(welcomeEmailTemplateContent); - -// Employee Activation Email Template -const employeeActivationTemplatePath = path.join( - __dirname, - "./templates/employeeActivation.mjml" -); -const employeeActivationTemplateContent = fs.readFileSync( - employeeActivationTemplatePath, - "utf8" -); -const employeeActivation = compile(employeeActivationTemplateContent); - -// No Incident This Week Template -const noIncidentsThisWeekTemplatePath = path.join( - __dirname, - "./templates/noIncidentsThisWeek.mjml" -); -const noIncidentsThisWeekTemplateContent = fs.readFileSync( - noIncidentsThisWeekTemplatePath, - "utf8" -); -const noIncidentsThisWeek = compile(noIncidentsThisWeekTemplateContent); - -// Server is Down Template -const serverIsDownTemplatePath = path.join( - __dirname, - "./templates/serverIsDown.mjml" -); -const serverIsDownTemplateContent = fs.readFileSync( - serverIsDownTemplatePath, - "utf8" -); -const serverIsDown = compile(serverIsDownTemplateContent); - -// Server is Up Template -const serverIsUpTemplatePath = path.join( - __dirname, - "./templates/serverIsUp.mjml" -); -const serverIsUpTemplateContent = fs.readFileSync( - serverIsUpTemplatePath, - "utf8" -); -const serverIsUp = compile(serverIsUpTemplateContent); - -// Password Reset Template -const passwordResetTemplatePath = path.join( - __dirname, - "./templates/passwordReset.mjml" -); -const passwordResetTemplateContent = fs.readFileSync( - passwordResetTemplatePath, - "utf8" -); -const passwordReset = compile(passwordResetTemplateContent); +const emailService = require("./service/emailService"); // Need to wrap server setup in a function to handle async nature of JobQueue const startApp = async () => { @@ -163,11 +92,11 @@ const startApp = async () => { // Nodemailer code here const transporter = nodemailer.createTransport({ - host: "127.0.0.1", - port: 1025, + host: process.env.EMAIL_SERVICE_HOST, + port: process.env.EMAIL_SERVICE_PORT, auth: { - user: "project.1", - pass: "secret.1", + user: process.env.EMAIL_SERVICE_USERNAME, + pass: process.env.EMAIL_SERVICE_PASSWORD, }, }); @@ -176,16 +105,12 @@ const startApp = async () => { // Replacing varibales const context = { name: "Alex" }; - // The Template must be replaced according to the scenario and the response we need to give - const mjml = welcomeEmailTemplate(context); - const html = mjml2html(mjml); - // Define mail options const mailOptions = { from: "BlueWave Uptime ", // sender address to: "muhammadkhalilzadeh1998@gmailc.com", // list of receivers subject: "Testing template emails", // Subject line - html: html, // html body + html: emailService.sendWelcomeEmail(context), // html body }; // Send mail with defined transport object diff --git a/Server/package-lock.json b/Server/package-lock.json index 17f15fc81..31ac86ad5 100644 --- a/Server/package-lock.json +++ b/Server/package-lock.json @@ -24,6 +24,7 @@ "mjml": "^1.3.4", "mongoose": "^8.3.3", "multer": "1.4.5-lts.1", + "nodemailer": "^6.9.14", "ping": "0.4.4", "sharp": "0.33.4", "winston": "^3.13.0" @@ -3816,6 +3817,14 @@ "node-gyp-build-optional-packages-test": "build-test.js" } }, + "node_modules/nodemailer": { + "version": "6.9.14", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.9.14.tgz", + "integrity": "sha512-Dobp/ebDKBvz91sbtRKhcznLThrKxKt97GI2FAlAyy+fk19j73Uz3sBXolVtmcXjaorivqsbbbjDY+Jkt4/bQA==", + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/nodemon": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.1.0.tgz", diff --git a/Server/package.json b/Server/package.json index 879aa80b5..bc3a4f299 100644 --- a/Server/package.json +++ b/Server/package.json @@ -26,6 +26,7 @@ "mjml": "^1.3.4", "mongoose": "^8.3.3", "multer": "1.4.5-lts.1", + "nodemailer": "^6.9.14", "ping": "0.4.4", "sharp": "0.33.4", "winston": "^3.13.0" diff --git a/Server/service/emailService.js b/Server/service/emailService.js new file mode 100644 index 000000000..235913b18 --- /dev/null +++ b/Server/service/emailService.js @@ -0,0 +1,125 @@ +const fs = require("fs"); +const path = require("path"); +const { compile } = require("handlebars"); +const { mjml2html } = require("mjml"); + +// Fetching Templates + +// Welcome Email Template +const welcomeEmailTemplatePath = path.join( + __dirname, + "../templates/welcomeEmail.mjml" +); +const welcomeEmailTemplateContent = fs.readFileSync( + welcomeEmailTemplatePath, + "utf8" +); +const welcomeEmailTemplate = compile(welcomeEmailTemplateContent); + +// Employee Activation Email Template +const employeeActivationTemplatePath = path.join( + __dirname, + "../templates/employeeActivation.mjml" +); +const employeeActivationTemplateContent = fs.readFileSync( + employeeActivationTemplatePath, + "utf8" +); +const employeeActivation = compile(employeeActivationTemplateContent); + +// No Incident This Week Template +const noIncidentsThisWeekTemplatePath = path.join( + __dirname, + "../templates/noIncidentsThisWeek.mjml" +); +const noIncidentsThisWeekTemplateContent = fs.readFileSync( + noIncidentsThisWeekTemplatePath, + "utf8" +); +const noIncidentsThisWeek = compile(noIncidentsThisWeekTemplateContent); + +// Server is Down Template +const serverIsDownTemplatePath = path.join( + __dirname, + "../templates/serverIsDown.mjml" +); +const serverIsDownTemplateContent = fs.readFileSync( + serverIsDownTemplatePath, + "utf8" +); +const serverIsDown = compile(serverIsDownTemplateContent); + +// Server is Up Template +const serverIsUpTemplatePath = path.join( + __dirname, + "../templates/serverIsUp.mjml" +); +const serverIsUpTemplateContent = fs.readFileSync( + serverIsUpTemplatePath, + "utf8" +); +const serverIsUp = compile(serverIsUpTemplateContent); + +// Password Reset Template +const passwordResetTemplatePath = path.join( + __dirname, + "../templates/passwordReset.mjml" +); +const passwordResetTemplateContent = fs.readFileSync( + passwordResetTemplatePath, + "utf8" +); +const passwordReset = compile(passwordResetTemplateContent); + +// *** Application specific functions *** + +function sendWelcomeEmail(context) { + const mjml = welcomeEmailTemplate(context); + const html = mjml2html(mjml); + + return html; +} + +function sendEmployeeActivationEmail(context) { + const mjml = employeeActivation(context); + const html = mjml2html(mjml); + + return html; +} + +function sendNoIncidentsThisWeekEmail(context) { + const mjml = noIncidentsThisWeek(context); + const html = mjml2html(mjml); + + return html; +} + +function sendServerIsDownEmail(context) { + const mjml = serverIsDown(context); + const html = mjml2html(mjml); + + return html; +} + +function sendServerIsUpEmail(context) { + const mjml = serverIsUp(context); + const html = mjml2html(mjml); + + return html; +} + +function sendPasswordResetEmail(context) { + const mjml = passwordReset(context); + const html = mjml2html(mjml); + + return html; +} + +module.exports = { + sendWelcomeEmail, + sendEmployeeActivationEmail, + sendNoIncidentsThisWeekEmail, + sendServerIsDownEmail, + sendServerIsUpEmail, + sendPasswordResetEmail, +};