Add library for template compilation; Add function to send out predefined verification mail

This commit is contained in:
Stefan-5422
2022-04-02 17:44:30 +02:00
parent 8f9a8ebdea
commit 5593652da8
2 changed files with 91 additions and 34 deletions
+58 -34
View File
@@ -1,43 +1,55 @@
import Handlebars, { template } from "handlebars";
import nodemailer from "nodemailer"; import nodemailer from "nodemailer";
import SMTPTransport from "nodemailer/lib/smtp-transport"; import SMTPTransport from "nodemailer/lib/smtp-transport";
let mailAccount = { user: process.env.MAILUSER + "@mail." + process.env.DOMAIN, pass: process.env.MAILPASSWORD }; import mjml from "./mjml";
let transporter = nodemailer.createTransport( export let mailAccount = {
new SMTPTransport({ user: process.env.MAILUSER + "@mail." + process.env.DOMAIN,
host: "localhost", pass: process.env.MAILPASSWORD || "password",
port: 587, };
secure: false,
auth: {
user: mailAccount.user,
pass: mailAccount.pass,
},
tls: {
rejectUnauthorized: false,
secureProtocol: "TLSv1_method",
},
})
);
if (process.env.DEV == "true") { let transporter =
console.log("test"); process.env.DEV == "true" || process.env.DOMAIN == undefined
(async () => { ? (async () => {
mailAccount = await nodemailer.createTestAccount(); mailAccount = await nodemailer.createTestAccount();
console.log(mailAccount); //mailAccount = {
transporter = nodemailer.createTransport({ // user: "",
host: "smtp.ethereal.email", // pass: "",
port: 587, //};
secure: false, // true for 465, false for other ports if (process.env.NODE_ENV != "test") {
auth: { console.log(mailAccount);
user: mailAccount.user, // generated ethereal user }
pass: mailAccount.pass, // generated ethereal password return nodemailer.createTransport({
}, host: "smtp.ethereal.email",
}); port: 587,
})(); secure: false, // true for 465, false for other ports
} auth: {
user: mailAccount.user, // generated ethereal user
pass: mailAccount.pass, // generated ethereal password
},
});
})()
: nodemailer.createTransport(
new SMTPTransport({
host: "localhost",
port: 587,
secure: false,
auth: {
user: mailAccount.user,
pass: mailAccount.pass,
},
tls: {
rejectUnauthorized: false,
secureProtocol: "TLSv1_method",
},
})
);
const sendMail = async (from: string, to: string, subject: string, text?: string, html?: string) => { const sendMail = async (from: string, to: string, subject: string, text?: string, html?: string) => {
return await transporter.sendMail({ return await (
await transporter
).sendMail({
from: from, from: from,
to: to, to: to,
subject: subject, subject: subject,
@@ -46,4 +58,16 @@ const sendMail = async (from: string, to: string, subject: string, text?: string
}); });
}; };
export default { sendMail, mailAccount }; export const verificationMail = async (to: string, id: string) => {
const raw = mjml.getTemplate("verify");
//TODO: Replace other handlebars with final values
const message = Handlebars.compile(raw);
const data = {};
const compiled = message(data);
sendMail(mailAccount.user, to, "Verify Email", undefined, compiled);
};
export default { sendMail };
+33
View File
@@ -0,0 +1,33 @@
import fs from "fs";
import path from "path";
import mjml from "mjml";
const template_folder = __dirname + "/../../resources/email/templates/";
const compileTemplates = () => {
console.log("Compiling templates: ");
fs.readdir(template_folder, (err, files) => {
if (err) console.log(err);
files.forEach((file) => {
if (path.extname(file) !== ".mjml") return;
let content = fs.readFileSync(template_folder + file);
let mjmlres = mjml(content.toString());
let hbs = template_folder + file.replace(".mjml", ".hbs");
fs.writeFileSync(hbs, mjmlres.html);
});
});
};
compileTemplates(); // This will compile all templates when this file is first included
const getTemplate = (name: string): string => {
let file = "";
file = fs.readFileSync(path.join(template_folder, name + ".hbs")).toString();
return file;
};
export default { getTemplate };