kutt/server/mail/mail.js

145 lines
4.3 KiB
JavaScript
Raw Normal View History

2024-08-11 18:41:03 +03:30
const nodemailer = require("nodemailer");
const path = require("node:path");
const fs = require("node:fs");
2020-01-30 18:51:52 +03:30
2024-08-11 18:41:03 +03:30
const { resetMailText, verifyMailText, changeEmailText } = require("./text");
const { CustomError } = require("../utils");
const env = require("../env");
const mailConfig = {
2020-01-30 18:51:52 +03:30
host: env.MAIL_HOST,
port: env.MAIL_PORT,
secure: env.MAIL_SECURE,
2020-10-12 19:02:41 +03:30
auth: env.MAIL_USER
? {
user: env.MAIL_USER,
pass: env.MAIL_PASSWORD
}
: undefined
};
const transporter = nodemailer.createTransport(mailConfig);
2020-01-30 18:51:52 +03:30
// Read email templates
const resetEmailTemplatePath = path.join(__dirname, "template-reset.html");
const verifyEmailTemplatePath = path.join(__dirname, "template-verify.html");
2024-09-12 14:26:39 +03:30
const changeEmailTemplatePath = path.join(__dirname,"template-change-email.html");
2024-09-23 13:45:39 +03:30
let resetEmailTemplate,
verifyEmailTemplate,
changeEmailTemplate;
// only read email templates if email is enabled
if (env.MAIL_ENABLED) {
resetEmailTemplate = fs
.readFileSync(resetEmailTemplatePath, { encoding: "utf-8" })
.replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
.replace(/{{site_name}}/gm, env.SITE_NAME);
verifyEmailTemplate = fs
.readFileSync(verifyEmailTemplatePath, { encoding: "utf-8" })
.replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
.replace(/{{site_name}}/gm, env.SITE_NAME);
changeEmailTemplate = fs
.readFileSync(changeEmailTemplatePath, { encoding: "utf-8" })
.replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
.replace(/{{site_name}}/gm, env.SITE_NAME);
}
2020-01-30 18:51:52 +03:30
2024-08-11 18:41:03 +03:30
async function verification(user) {
2024-09-23 13:45:39 +03:30
if (!env.MAIL_ENABLED) {
throw new Error("Attempting to send verification email but email is not enabled.");
};
2020-01-30 18:51:52 +03:30
const mail = await transporter.sendMail({
from: env.MAIL_FROM || env.MAIL_USER,
to: user.email,
subject: "Verify your account",
2020-02-05 18:27:08 +03:30
text: verifyMailText
.replace(/{{verification}}/gim, user.verification_token)
.replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
.replace(/{{site_name}}/gm, env.SITE_NAME),
html: verifyEmailTemplate
.replace(/{{verification}}/gim, user.verification_token)
.replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
.replace(/{{site_name}}/gm, env.SITE_NAME)
2020-01-30 18:51:52 +03:30
});
if (!mail.accepted.length) {
throw new CustomError("Couldn't send verification email. Try again later.");
}
2024-08-11 18:41:03 +03:30
}
2020-01-30 18:51:52 +03:30
2024-08-11 18:41:03 +03:30
async function changeEmail(user) {
2024-09-23 13:45:39 +03:30
if (!env.MAIL_ENABLED) {
throw new Error("Attempting to send change email token but email is not enabled.");
};
2020-09-19 18:02:32 +04:30
const mail = await transporter.sendMail({
from: env.MAIL_FROM || env.MAIL_USER,
to: user.change_email_address,
subject: "Verify your new email address",
text: changeEmailText
.replace(/{{verification}}/gim, user.change_email_token)
.replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
.replace(/{{site_name}}/gm, env.SITE_NAME),
html: changeEmailTemplate
.replace(/{{verification}}/gim, user.change_email_token)
.replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
.replace(/{{site_name}}/gm, env.SITE_NAME)
});
2024-08-11 18:41:03 +03:30
2020-09-19 18:02:32 +04:30
if (!mail.accepted.length) {
throw new CustomError("Couldn't send verification email. Try again later.");
}
2024-08-11 18:41:03 +03:30
}
2020-09-19 18:02:32 +04:30
2024-08-11 18:41:03 +03:30
async function resetPasswordToken(user) {
2024-09-23 13:45:39 +03:30
if (!env.MAIL_ENABLED) {
throw new Error("Attempting to send reset password email but email is not enabled.");
};
2020-01-30 18:51:52 +03:30
const mail = await transporter.sendMail({
2020-02-05 18:27:08 +03:30
from: env.MAIL_FROM || env.MAIL_USER,
2020-01-30 18:51:52 +03:30
to: user.email,
subject: "Reset your password",
text: resetMailText
.replace(/{{resetpassword}}/gm, user.reset_password_token)
.replace(/{{domain}}/gm, env.DEFAULT_DOMAIN),
html: resetEmailTemplate
.replace(/{{resetpassword}}/gm, user.reset_password_token)
.replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
});
2024-09-23 13:45:39 +03:30
2020-01-30 18:51:52 +03:30
if (!mail.accepted.length) {
throw new CustomError(
"Couldn't send reset password email. Try again later."
);
}
2024-08-11 18:41:03 +03:30
}
2024-09-08 14:10:02 +03:30
async function sendReportEmail(link) {
2024-09-23 13:45:39 +03:30
if (!env.MAIL_ENABLED) {
throw new Error("Attempting to send report email but email is not enabled.");
};
2024-09-08 14:10:02 +03:30
const mail = await transporter.sendMail({
from: env.MAIL_FROM || env.MAIL_USER,
to: env.REPORT_EMAIL,
subject: "[REPORT]",
text: link,
html: link
});
if (!mail.accepted.length) {
throw new CustomError("Couldn't submit the report. Try again later.");
}
}
2024-08-11 18:41:03 +03:30
module.exports = {
changeEmail,
verification,
resetPasswordToken,
2024-09-08 14:10:02 +03:30
sendReportEmail,
2024-08-11 18:41:03 +03:30
}