Add handlebars;

This commit is contained in:
Stefan-5422
2022-03-20 14:58:27 +01:00
parent a84fcdce8f
commit c96537f5b4
3 changed files with 135 additions and 40 deletions
+52 -40
View File
@@ -1,44 +1,51 @@
import { writeFile } from "fs";
import Handlebars, { template } from "handlebars";
import { emailClient } from "./redis";
import nodemailer from "nodemailer";
import SMTPTransport from "nodemailer/lib/smtp-transport";
import mjml from "./mjml"
import mjml from "./mjml";
import { randomUUID } from "crypto";
export let mailAccount = { user: process.env.MAILUSER + "@mail." + process.env.DOMAIN, pass: process.env.MAILPASSWORD };
let transporter = (process.env.DEV == "true" || process.env.DOMAIN == undefined) ? (async () => {
mailAccount = await nodemailer.createTestAccount();
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",
},
})
);
let transporter =
process.env.DEV == "true" || process.env.DOMAIN == undefined
? (async () => {
mailAccount = await nodemailer.createTestAccount();
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 (await transporter).sendMail({
return await (
await transporter
).sendMail({
from: from,
to: to,
subject: subject,
@@ -47,14 +54,19 @@ const sendMail = async (from: string, to: string, subject: string, text?: string
});
};
export const verificationMail = async (to: string, ) => {
const message = mjml.getTemplate("emailVerification")
//TODO: replace handlebars with code
export const verificationMail = async (to: string, eventName: string, verificationLink: string) => {
const raw = mjml.getTemplate("emailVerification");
//TODO: Replace other handlebars with final values
const message = Handlebars.compile(raw);
const data = { eventName, verificationLink };
const compiled = message(data);
sendMail(mailAccount.user, to, "Verify Email", undefined, compiled);
//TODO: Implement verification codes
//TODO: Write endpoints to verify codes
};
sendMail(mailAccount.user,to,"Verify Email",undefined,message);
}
export default { sendMail};
export default { sendMail };