mirror of
https://github.com/GithubOrgProjectPiza/server.git
synced 2026-09-04 08:36:10 +02:00
Add library for template compilation; Add function to send out predefined verification mail
This commit is contained in:
+58
-34
@@ -1,43 +1,55 @@
|
||||
import Handlebars, { template } from "handlebars";
|
||||
import nodemailer from "nodemailer";
|
||||
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(
|
||||
new SMTPTransport({
|
||||
host: "localhost",
|
||||
port: 587,
|
||||
secure: false,
|
||||
auth: {
|
||||
user: mailAccount.user,
|
||||
pass: mailAccount.pass,
|
||||
},
|
||||
tls: {
|
||||
rejectUnauthorized: false,
|
||||
secureProtocol: "TLSv1_method",
|
||||
},
|
||||
})
|
||||
);
|
||||
export let mailAccount = {
|
||||
user: process.env.MAILUSER + "@mail." + process.env.DOMAIN,
|
||||
pass: process.env.MAILPASSWORD || "password",
|
||||
};
|
||||
|
||||
if (process.env.DEV == "true") {
|
||||
console.log("test");
|
||||
(async () => {
|
||||
mailAccount = await nodemailer.createTestAccount();
|
||||
console.log(mailAccount);
|
||||
transporter = 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
|
||||
},
|
||||
});
|
||||
})();
|
||||
}
|
||||
let transporter =
|
||||
process.env.DEV == "true" || process.env.DOMAIN == undefined
|
||||
? (async () => {
|
||||
mailAccount = await nodemailer.createTestAccount();
|
||||
//mailAccount = {
|
||||
// user: "",
|
||||
// pass: "",
|
||||
//};
|
||||
if (process.env.NODE_ENV != "test") {
|
||||
console.log(mailAccount);
|
||||
}
|
||||
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) => {
|
||||
return await transporter.sendMail({
|
||||
return await (
|
||||
await transporter
|
||||
).sendMail({
|
||||
from: from,
|
||||
to: to,
|
||||
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 };
|
||||
|
||||
@@ -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 };
|
||||
Reference in New Issue
Block a user