From 505c6bf5de52411732fab809a0ef3619555b2110 Mon Sep 17 00:00:00 2001 From: MuhammadKhalilzadeh Date: Fri, 19 Jul 2024 21:44:40 +0330 Subject: [PATCH] Nodemailer implemented --- Server/index.js | 113 ++++++++++++++++++ Server/package-lock.json | 51 ++++++++ Server/package.json | 1 + Server/templates/employeeActivation.mjml | 6 +- Server/templates/noIncidentsThisWeek.mjml | 4 +- Server/templates/passwordReset.mjml | 90 +++++++------- Server/templates/serverIsDown.mjml | 126 +++++++++----------- Server/templates/serverIsUp.mjml | 138 ++++++++++------------ Server/templates/welcomeEmail.mjml | 92 +++++++-------- 9 files changed, 371 insertions(+), 250 deletions(-) diff --git a/Server/index.js b/Server/index.js index 9cd8d57ec..cd25e8399 100644 --- a/Server/index.js +++ b/Server/index.js @@ -13,6 +13,80 @@ const { handleErrors } = require("./middleware/handleErrors"); const queueRouter = require("./routes/queueRoute"); 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); // Need to wrap server setup in a function to handle async nature of JobQueue const startApp = async () => { @@ -87,6 +161,45 @@ const startApp = async () => { } }); + // Nodemailer code here + const transporter = nodemailer.createTransport({ + host: "127.0.0.1", + port: 1025, + auth: { + user: "project.1", + pass: "secret.1", + }, + }); + + app.use("/api/v1/mail", (req, res) => { + console.log("Started"); + // 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 + }; + + // Send mail with defined transport object + transporter.sendMail(mailOptions, (error, info) => { + if (error) { + return res + .status(500) + .send({ message: "Error sending email", error: error }); + } + console.log(info); + res.status(200).send({ message: "Email sent successfully", info: info }); + }); + }); + /** * Error handler middleware * Should be called last diff --git a/Server/package-lock.json b/Server/package-lock.json index 8c437da48..17f15fc81 100644 --- a/Server/package-lock.json +++ b/Server/package-lock.json @@ -16,6 +16,7 @@ "cors": "^2.8.5", "dotenv": "^16.4.5", "express": "^4.19.2", + "handlebars": "^4.7.8", "helmet": "^7.1.0", "joi": "^17.13.1", "jsonwebtoken": "9.0.2", @@ -2405,6 +2406,51 @@ "resolved": "https://registry.npmjs.org/graceful-readlink/-/graceful-readlink-1.0.1.tgz", "integrity": "sha512-8tLu60LgxF6XpdbK8OW3FA+IfTNBn1ZHGHKF4KQbEeSkajYw5PlYJcKluntgegDPTg8UkHjpet1T82vk6TQ68w==" }, + "node_modules/handlebars": { + "version": "4.7.8", + "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.7.8.tgz", + "integrity": "sha512-vafaFqs8MZkRrSX7sFVUdo3ap/eNiLnb4IakshzvP56X5Nr1iGKAIqdX6tMlm6HcNRIkr6AxO5jFEoJzzpT8aQ==", + "dependencies": { + "minimist": "^1.2.5", + "neo-async": "^2.6.2", + "source-map": "^0.6.1", + "wordwrap": "^1.0.0" + }, + "bin": { + "handlebars": "bin/handlebars" + }, + "engines": { + "node": ">=0.4.7" + }, + "optionalDependencies": { + "uglify-js": "^3.1.4" + } + }, + "node_modules/handlebars/node_modules/source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/handlebars/node_modules/uglify-js": { + "version": "3.19.0", + "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.19.0.tgz", + "integrity": "sha512-wNKHUY2hYYkf6oSFfhwwiHo4WCHzHmzcXsqXYTN9ja3iApYIFbb2U6ics9hBcYLHcYGQoAlwnZlTrf3oF+BL/Q==", + "optional": true, + "bin": { + "uglifyjs": "bin/uglifyjs" + }, + "engines": { + "node": ">=0.8.0" + } + }, + "node_modules/handlebars/node_modules/wordwrap": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-1.0.0.tgz", + "integrity": "sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==" + }, "node_modules/har-schema": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/har-schema/-/har-schema-2.0.0.tgz", @@ -3703,6 +3749,11 @@ "node": ">= 0.6" } }, + "node_modules/neo-async": { + "version": "2.6.2", + "resolved": "https://registry.npmjs.org/neo-async/-/neo-async-2.6.2.tgz", + "integrity": "sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==" + }, "node_modules/node-abort-controller": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/node-abort-controller/-/node-abort-controller-3.1.1.tgz", diff --git a/Server/package.json b/Server/package.json index d6d8c8d72..879aa80b5 100644 --- a/Server/package.json +++ b/Server/package.json @@ -18,6 +18,7 @@ "cors": "^2.8.5", "dotenv": "^16.4.5", "express": "^4.19.2", + "handlebars": "^4.7.8", "helmet": "^7.1.0", "joi": "^17.13.1", "jsonwebtoken": "9.0.2", diff --git a/Server/templates/employeeActivation.mjml b/Server/templates/employeeActivation.mjml index 99d08d489..7fcaebfc2 100644 --- a/Server/templates/employeeActivation.mjml +++ b/Server/templates/employeeActivation.mjml @@ -16,10 +16,10 @@ -

Hello Alex!

+

Hello {{name}}!

One of the admins created an account for you on the BlueWave Uptime server.

You can go ahead and create your account using this link.

-

https://example.com

+

{{link}}

Thank you.

@@ -31,4 +31,4 @@
- + \ No newline at end of file diff --git a/Server/templates/noIncidentsThisWeek.mjml b/Server/templates/noIncidentsThisWeek.mjml index 0c2801e89..960a876fe 100644 --- a/Server/templates/noIncidentsThisWeek.mjml +++ b/Server/templates/noIncidentsThisWeek.mjml @@ -20,7 +20,7 @@ -

Hello Alex!

+

Hello {{name}}!

There were no incidents this week. Good job!

Current monitors:

Google: 100% availability

@@ -35,4 +35,4 @@
- + \ No newline at end of file diff --git a/Server/templates/passwordReset.mjml b/Server/templates/passwordReset.mjml index 7043401f3..de1e23138 100644 --- a/Server/templates/passwordReset.mjml +++ b/Server/templates/passwordReset.mjml @@ -1,51 +1,43 @@ - - - - - - - - - - - - - Message from BlueWave Uptime Service - - - - - - -

Hello Alex!

-

- You are receiving this email because a password reset request - has been made for gorkem.cetin@bluewavelabs.ca. Please use the - code below on the site to reset your password. -

-

- Verification Code: 4CCDFC45 -

-

If you didn't request this, please ignore this email.

+ + + + + + + + + + + + + Message from BlueWave Uptime Service + + + + + + +

Hello {{name}}!

+

+ You are receiving this email because a password reset request + has been made for gorkem.cetin@bluewavelabs.ca. Please use the + code below on the site to reset your password. +

+

+ Verification Code: {{verificationCode}} +

+

If you didn't request this, please ignore this email.

-

Thank you.

-
-
- - - -

This email was sent by BlueWave Uptime.

-
-
-
-
-
\ No newline at end of file +

Thank you.

+ + + + + +

This email was sent by BlueWave Uptime.

+
+
+ + + \ No newline at end of file diff --git a/Server/templates/serverIsDown.mjml b/Server/templates/serverIsDown.mjml index 95df602f5..3caa6549c 100644 --- a/Server/templates/serverIsDown.mjml +++ b/Server/templates/serverIsDown.mjml @@ -1,71 +1,57 @@ - - - - - - - - - - - - - Message from BlueWave Uptime Service - - - - - Google.com is down - - - - - - - -

Hello Alex!

-

- We detected an incident on one of your monitors. Your service is - currently down. We'll send a message to you once it is up again. -

-

- Monitor name: Google.com -

-

- URL: https://google.com -

-

- Problem: Connection timeout -

-

- Start date: Jun 8, 2024 8:15pm -

-
-
- - - - View incident details - - -

This email was sent by BlueWave Uptime.

-
-
-
-
-
\ No newline at end of file + + + + + + + + + + + + + Message from BlueWave Uptime Service + + + + + Google.com is down + + + + + + + +

Hello {{name}}!

+

+ We detected an incident on one of your monitors. Your service is + currently down. We'll send a message to you once it is up again. +

+

+ Monitor name: {{monitor}} +

+

+ URL: {{url}} +

+

+ Problem: {{problem}} +

+

+ Start date: {{startDate}} +

+
+
+ + + + View incident details + + +

This email was sent by BlueWave Uptime.

+
+
+
+
+ \ No newline at end of file diff --git a/Server/templates/serverIsUp.mjml b/Server/templates/serverIsUp.mjml index aa2caa5ba..9e73a8559 100644 --- a/Server/templates/serverIsUp.mjml +++ b/Server/templates/serverIsUp.mjml @@ -1,77 +1,63 @@ - - - - - - - - - - - - - Message from BlueWave Uptime Service - - - - - Google.com is up - - - - - - - -

Hello Alex!

-

- Your latest incident is resolved and your monitored service is - up again. -

-

- Monitor name: Google.com -

-

- URL: https://google.com -

-

- Problem: Connection timeout -

-

- Start date: Jun 8, 2024 8:15pm -

-

- Resolved date: Jun 8, 2024 8:15pm -

-

- Duration:3 minutes and 15 seconds -

-
-
- - - - View incident details - - -

This email was sent by BlueWave Uptime.

-
-
-
-
-
\ No newline at end of file + + + + + + + + + + + + + Message from BlueWave Uptime Service + + + + + {{monitor}} is up + + + + + + + +

Hello {{name}}!

+

+ Your latest incident is resolved and your monitored service is + up again. +

+

+ Monitor name: {{monitor}} +

+

+ URL: {{url}} +

+

+ Problem: {{problem}} +

+

+ Start date: {{startDate}} +

+

+ Resolved date: {{resolvedDate}} +

+

+ Duration:{{duration}} +

+
+
+ + + + View incident details + + +

This email was sent by BlueWave Uptime.

+
+
+
+
+ \ No newline at end of file diff --git a/Server/templates/welcomeEmail.mjml b/Server/templates/welcomeEmail.mjml index 62e5333d8..cb71bb34c 100644 --- a/Server/templates/welcomeEmail.mjml +++ b/Server/templates/welcomeEmail.mjml @@ -1,51 +1,43 @@ - - - - - - - - - - - - - Message from BlueWave Uptime Service - - - - - - -

Hello Alex!

-

- Thank you for trying out BlueWave Uptime! We developed it with - great care to meet our own needs, and we're excited to share it - with you. -

-

- BlueWave Uptime is an automated way of checking whether a - service such as a website or an application is available or not. -

-

We hope you find our service as valuable as we do.

-

Thank you.

-
-
- - - -

This email was sent by BlueWave Uptime.

-
-
-
-
-
\ No newline at end of file + + + + + + + + + + + + + Message from BlueWave Uptime Service + + + + + + +

Hello {{name}}!

+

+ Thank you for trying out BlueWave Uptime! We developed it with + great care to meet our own needs, and we're excited to share it + with you. +

+

+ BlueWave Uptime is an automated way of checking whether a + service such as a website or an application is available or not. +

+

We hope you find our service as valuable as we do.

+

Thank you.

+
+
+ + + +

This email was sent by BlueWave Uptime.

+
+
+
+
+ \ No newline at end of file