emailService.js created, env variables added

This commit is contained in:
MuhammadKhalilzadeh committed 2024-07-19 22:39:12 +03:30
1 parent 505c6bf5de
commit d541dfb57b
4 files changed
+141 -81

No files matched your search

+6 -81
View File
@@ -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 <bluewaveuptime@gmail.com>", // 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