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 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 };