Nodemailer implemented

This commit is contained in:
MuhammadKhalilzadeh committed 2024-07-19 21:44:40 +03:30
1 parent 87e0bb907d
commit 505c6bf5de
9 files changed
+199 -78

No files matched your search

+113
View File
@@ -13,6 +13,80 @@ const { handleErrors } = require("./middleware/handleErrors");
const queueRouter = require("./routes/queueRoute"); const queueRouter = require("./routes/queueRoute");
const JobQueue = require("./service/jobQueue"); const JobQueue = require("./service/jobQueue");
const pageSpeedCheckRouter = require("./routes/pageSpeedCheckRoute"); 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 // Need to wrap server setup in a function to handle async nature of JobQueue
const startApp = async () => { 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 <bluewaveuptime@gmail.com>", // 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 * Error handler middleware
* Should be called last * Should be called last
+51
View File
@@ -16,6 +16,7 @@
"cors": "^2.8.5", "cors": "^2.8.5",
"dotenv": "^16.4.5", "dotenv": "^16.4.5",
"express": "^4.19.2", "express": "^4.19.2",
"handlebars": "^4.7.8",
"helmet": "^7.1.0", "helmet": "^7.1.0",
"joi": "^17.13.1", "joi": "^17.13.1",
"jsonwebtoken": "9.0.2", "jsonwebtoken": "9.0.2",
@@ -2405,6 +2406,51 @@
"resolved": "https://registry.npmjs.org/graceful-readlink/-/graceful-readlink-1.0.1.tgz", "resolved": "https://registry.npmjs.org/graceful-readlink/-/graceful-readlink-1.0.1.tgz",
"integrity": "sha512-8tLu60LgxF6XpdbK8OW3FA+IfTNBn1ZHGHKF4KQbEeSkajYw5PlYJcKluntgegDPTg8UkHjpet1T82vk6TQ68w==" "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": { "node_modules/har-schema": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/har-schema/-/har-schema-2.0.0.tgz", "resolved": "https://registry.npmjs.org/har-schema/-/har-schema-2.0.0.tgz",
@@ -3703,6 +3749,11 @@
"node": ">= 0.6" "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": { "node_modules/node-abort-controller": {
"version": "3.1.1", "version": "3.1.1",
"resolved": "https://registry.npmjs.org/node-abort-controller/-/node-abort-controller-3.1.1.tgz", "resolved": "https://registry.npmjs.org/node-abort-controller/-/node-abort-controller-3.1.1.tgz",
+1
View File
@@ -18,6 +18,7 @@
"cors": "^2.8.5", "cors": "^2.8.5",
"dotenv": "^16.4.5", "dotenv": "^16.4.5",
"express": "^4.19.2", "express": "^4.19.2",
"handlebars": "^4.7.8",
"helmet": "^7.1.0", "helmet": "^7.1.0",
"joi": "^17.13.1", "joi": "^17.13.1",
"jsonwebtoken": "9.0.2", "jsonwebtoken": "9.0.2",
+2 -2
View File
@@ -16,10 +16,10 @@
<mj-section> <mj-section>
<mj-column width="100%"> <mj-column width="100%">
<mj-text> <mj-text>
<p>Hello Alex!</p> <p>Hello {{name}}!</p>
<p>One of the admins created an account for you on the BlueWave Uptime server.</p> <p>One of the admins created an account for you on the BlueWave Uptime server.</p>
<p>You can go ahead and create your account using this link.</p> <p>You can go ahead and create your account using this link.</p>
<p><a href="https://example.com">https://example.com</a></p> <p><a href={{link}}>{{link}}</a></p>
<p>Thank you.</p> <p>Thank you.</p>
</mj-text> </mj-text>
</mj-column> </mj-column>
+1 -1
View File
@@ -20,7 +20,7 @@
<mj-section> <mj-section>
<mj-column width="100%"> <mj-column width="100%">
<mj-text> <mj-text>
<p>Hello Alex!</p> <p>Hello {{name}}!</p>
<p>There were no incidents this week. Good job!</p> <p>There were no incidents this week. Good job!</p>
<p><b>Current monitors:</b></p> <p><b>Current monitors:</b></p>
<p><b>Google:</b> 100% availability</p> <p><b>Google:</b> 100% availability</p>
+5 -13
View File
@@ -1,17 +1,9 @@
<mjml> <mjml>
<mj-head> <mj-head>
<mj-font <mj-font name="Roboto" href="https://fonts.googleapis.com/css?family=Roboto:300,500"></mj-font>
name="Roboto"
href="https://fonts.googleapis.com/css?family=Roboto:300,500"
></mj-font>
<mj-attributes> <mj-attributes>
<mj-all font-family="Roboto, Helvetica, sans-serif"></mj-all> <mj-all font-family="Roboto, Helvetica, sans-serif"></mj-all>
<mj-text <mj-text font-weight="300" font-size="16px" color="#616161" line-height="24px"></mj-text>
font-weight="300"
font-size="16px"
color="#616161"
line-height="24px"
></mj-text>
<mj-section padding="0px"></mj-section> <mj-section padding="0px"></mj-section>
</mj-attributes> </mj-attributes>
</mj-head> </mj-head>
@@ -26,14 +18,14 @@
<mj-section> <mj-section>
<mj-column width="100%"> <mj-column width="100%">
<mj-text> <mj-text>
<p>Hello Alex!</p> <p>Hello {{name}}!</p>
<p> <p>
You are receiving this email because a password reset request You are receiving this email because a password reset request
has been made for gorkem.cetin@bluewavelabs.ca. Please use the has been made for gorkem.cetin@bluewavelabs.ca. Please use the
code below on the site to reset your password. code below on the site to reset your password.
</p> </p>
<p> <p>
Verification Code: <b>4CCDFC45</b> Verification Code: <b>{{verificationCode}}</b>
</p> </p>
<p>If you didn't request this, please ignore this email.</p> <p>If you didn't request this, please ignore this email.</p>
@@ -48,4 +40,4 @@
</mj-column> </mj-column>
</mj-section> </mj-section>
</mj-body> </mj-body>
</mjml> </mjml>
+9 -23
View File
@@ -1,17 +1,9 @@
<mjml> <mjml>
<mj-head> <mj-head>
<mj-font <mj-font name="Roboto" href="https://fonts.googleapis.com/css?family=Roboto:300,500"></mj-font>
name="Roboto"
href="https://fonts.googleapis.com/css?family=Roboto:300,500"
></mj-font>
<mj-attributes> <mj-attributes>
<mj-all font-family="Roboto, Helvetica, sans-serif"></mj-all> <mj-all font-family="Roboto, Helvetica, sans-serif"></mj-all>
<mj-text <mj-text font-weight="300" font-size="16px" color="#616161" line-height="24px"></mj-text>
font-weight="300"
font-size="16px"
color="#616161"
line-height="24px"
></mj-text>
<mj-section padding="0px"></mj-section> <mj-section padding="0px"></mj-section>
</mj-attributes> </mj-attributes>
</mj-head> </mj-head>
@@ -23,13 +15,7 @@
</mj-text> </mj-text>
</mj-column> </mj-column>
<mj-column width="45%" padding-top="20px"> <mj-column width="45%" padding-top="20px">
<mj-text <mj-text align="center" font-weight="500" padding="0px" font-size="18px" color="red">
align="center"
font-weight="500"
padding="0px"
font-size="18px"
color="red"
>
Google.com is down Google.com is down
</mj-text> </mj-text>
<mj-divider border-width="2px" border-color="#616161"></mj-divider> <mj-divider border-width="2px" border-color="#616161"></mj-divider>
@@ -38,22 +24,22 @@
<mj-section> <mj-section>
<mj-column width="100%"> <mj-column width="100%">
<mj-text> <mj-text>
<p>Hello Alex!</p> <p>Hello {{name}}!</p>
<p> <p>
We detected an incident on one of your monitors. Your service is 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. currently down. We'll send a message to you once it is up again.
</p> </p>
<p> <p>
<b>Monitor name:</b> Google.com <b>Monitor name:</b> {{monitor}}
</p> </p>
<p> <p>
<b>URL:</b> https://google.com <b>URL:</b> {{url}}
</p> </p>
<p> <p>
<b>Problem:</b> Connection timeout <b>Problem:</b> {{problem}}
</p> </p>
<p> <p>
<b>Start date:</b> Jun 8, 2024 8:15pm <b>Start date:</b> {{startDate}}
</p> </p>
</mj-text> </mj-text>
</mj-column> </mj-column>
@@ -68,4 +54,4 @@
</mj-column> </mj-column>
</mj-section> </mj-section>
</mj-body> </mj-body>
</mjml> </mjml>
+13 -27
View File
@@ -1,17 +1,9 @@
<mjml> <mjml>
<mj-head> <mj-head>
<mj-font <mj-font name="Roboto" href="https://fonts.googleapis.com/css?family=Roboto:300,500"></mj-font>
name="Roboto"
href="https://fonts.googleapis.com/css?family=Roboto:300,500"
></mj-font>
<mj-attributes> <mj-attributes>
<mj-all font-family="Roboto, Helvetica, sans-serif"></mj-all> <mj-all font-family="Roboto, Helvetica, sans-serif"></mj-all>
<mj-text <mj-text font-weight="300" font-size="16px" color="#616161" line-height="24px"></mj-text>
font-weight="300"
font-size="16px"
color="#616161"
line-height="24px"
></mj-text>
<mj-section padding="0px"></mj-section> <mj-section padding="0px"></mj-section>
</mj-attributes> </mj-attributes>
</mj-head> </mj-head>
@@ -23,14 +15,8 @@
</mj-text> </mj-text>
</mj-column> </mj-column>
<mj-column width="45%" padding-top="20px"> <mj-column width="45%" padding-top="20px">
<mj-text <mj-text align="center" font-weight="500" padding="0px" font-size="18px" color="green">
align="center" {{monitor}} is up
font-weight="500"
padding="0px"
font-size="18px"
color="green"
>
Google.com is up
</mj-text> </mj-text>
<mj-divider border-width="2px" border-color="#616161"></mj-divider> <mj-divider border-width="2px" border-color="#616161"></mj-divider>
</mj-column> </mj-column>
@@ -38,28 +24,28 @@
<mj-section> <mj-section>
<mj-column width="100%"> <mj-column width="100%">
<mj-text> <mj-text>
<p>Hello Alex!</p> <p>Hello {{name}}!</p>
<p> <p>
Your latest incident is resolved and your monitored service is Your latest incident is resolved and your monitored service is
up again. up again.
</p> </p>
<p> <p>
<b>Monitor name:</b> Google.com <b>Monitor name:</b> {{monitor}}
</p> </p>
<p> <p>
<b>URL:</b> https://google.com <b>URL:</b> {{url}}
</p> </p>
<p> <p>
<b>Problem:</b> Connection timeout <b>Problem:</b> {{problem}}
</p> </p>
<p> <p>
<b>Start date:</b> Jun 8, 2024 8:15pm <b>Start date:</b> {{startDate}}
</p> </p>
<p> <p>
<b>Resolved date:</b> Jun 8, 2024 8:15pm <b>Resolved date:</b> {{resolvedDate}}
</p> </p>
<p> <p>
<b>Duration:</b>3 minutes and 15 seconds <b>Duration:</b>{{duration}}
</p> </p>
</mj-text> </mj-text>
</mj-column> </mj-column>
@@ -74,4 +60,4 @@
</mj-column> </mj-column>
</mj-section> </mj-section>
</mj-body> </mj-body>
</mjml> </mjml>
+4 -12
View File
@@ -1,17 +1,9 @@
<mjml> <mjml>
<mj-head> <mj-head>
<mj-font <mj-font name="Roboto" href="https://fonts.googleapis.com/css?family=Roboto:300,500"></mj-font>
name="Roboto"
href="https://fonts.googleapis.com/css?family=Roboto:300,500"
></mj-font>
<mj-attributes> <mj-attributes>
<mj-all font-family="Roboto, Helvetica, sans-serif"></mj-all> <mj-all font-family="Roboto, Helvetica, sans-serif"></mj-all>
<mj-text <mj-text font-weight="300" font-size="16px" color="#616161" line-height="24px"></mj-text>
font-weight="300"
font-size="16px"
color="#616161"
line-height="24px"
></mj-text>
<mj-section padding="0px"></mj-section> <mj-section padding="0px"></mj-section>
</mj-attributes> </mj-attributes>
</mj-head> </mj-head>
@@ -26,7 +18,7 @@
<mj-section> <mj-section>
<mj-column width="100%"> <mj-column width="100%">
<mj-text> <mj-text>
<p>Hello Alex!</p> <p>Hello {{name}}!</p>
<p> <p>
Thank you for trying out BlueWave Uptime! We developed it with 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 great care to meet our own needs, and we're excited to share it
@@ -48,4 +40,4 @@
</mj-column> </mj-column>
</mj-section> </mj-section>
</mj-body> </mj-body>
</mjml> </mjml>