emailService.js created, env variables added
This commit is contained in:
1 parent
505c6bf5de
commit
d541dfb57b
4 files changed
+141
-81
No files matched your search
+6
-81
@@ -15,78 +15,7 @@ const JobQueue = require("./service/jobQueue");
|
|||||||
const pageSpeedCheckRouter = require("./routes/pageSpeedCheckRoute");
|
const pageSpeedCheckRouter = require("./routes/pageSpeedCheckRoute");
|
||||||
const nodemailer = require("nodemailer");
|
const nodemailer = require("nodemailer");
|
||||||
|
|
||||||
const fs = require("fs");
|
const emailService = require("./service/emailService");
|
||||||
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 () => {
|
||||||
@@ -163,11 +92,11 @@ const startApp = async () => {
|
|||||||
|
|
||||||
// Nodemailer code here
|
// Nodemailer code here
|
||||||
const transporter = nodemailer.createTransport({
|
const transporter = nodemailer.createTransport({
|
||||||
host: "127.0.0.1",
|
host: process.env.EMAIL_SERVICE_HOST,
|
||||||
port: 1025,
|
port: process.env.EMAIL_SERVICE_PORT,
|
||||||
auth: {
|
auth: {
|
||||||
user: "project.1",
|
user: process.env.EMAIL_SERVICE_USERNAME,
|
||||||
pass: "secret.1",
|
pass: process.env.EMAIL_SERVICE_PASSWORD,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -176,16 +105,12 @@ const startApp = async () => {
|
|||||||
// Replacing varibales
|
// Replacing varibales
|
||||||
const context = { name: "Alex" };
|
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
|
// Define mail options
|
||||||
const mailOptions = {
|
const mailOptions = {
|
||||||
from: "BlueWave Uptime <bluewaveuptime@gmail.com>", // sender address
|
from: "BlueWave Uptime <bluewaveuptime@gmail.com>", // sender address
|
||||||
to: "muhammadkhalilzadeh1998@gmailc.com", // list of receivers
|
to: "muhammadkhalilzadeh1998@gmailc.com", // list of receivers
|
||||||
subject: "Testing template emails", // Subject line
|
subject: "Testing template emails", // Subject line
|
||||||
html: html, // html body
|
html: emailService.sendWelcomeEmail(context), // html body
|
||||||
};
|
};
|
||||||
|
|
||||||
// Send mail with defined transport object
|
// Send mail with defined transport object
|
||||||
|
|||||||
Generated
+9
@@ -24,6 +24,7 @@
|
|||||||
"mjml": "^1.3.4",
|
"mjml": "^1.3.4",
|
||||||
"mongoose": "^8.3.3",
|
"mongoose": "^8.3.3",
|
||||||
"multer": "1.4.5-lts.1",
|
"multer": "1.4.5-lts.1",
|
||||||
|
"nodemailer": "^6.9.14",
|
||||||
"ping": "0.4.4",
|
"ping": "0.4.4",
|
||||||
"sharp": "0.33.4",
|
"sharp": "0.33.4",
|
||||||
"winston": "^3.13.0"
|
"winston": "^3.13.0"
|
||||||
@@ -3816,6 +3817,14 @@
|
|||||||
"node-gyp-build-optional-packages-test": "build-test.js"
|
"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": {
|
"node_modules/nodemon": {
|
||||||
"version": "3.1.0",
|
"version": "3.1.0",
|
||||||
"resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.1.0.tgz",
|
||||||
|
|||||||
@@ -26,6 +26,7 @@
|
|||||||
"mjml": "^1.3.4",
|
"mjml": "^1.3.4",
|
||||||
"mongoose": "^8.3.3",
|
"mongoose": "^8.3.3",
|
||||||
"multer": "1.4.5-lts.1",
|
"multer": "1.4.5-lts.1",
|
||||||
|
"nodemailer": "^6.9.14",
|
||||||
"ping": "0.4.4",
|
"ping": "0.4.4",
|
||||||
"sharp": "0.33.4",
|
"sharp": "0.33.4",
|
||||||
"winston": "^3.13.0"
|
"winston": "^3.13.0"
|
||||||
|
|||||||
@@ -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,
|
||||||
|
};
|
||||||
Reference in new issue
Block a user