|
|
@@ -24,20 +24,33 @@ const transporter = nodemailer.createTransport(mailConfig);
|
|
|
const resetEmailTemplatePath = path.join(__dirname, "template-reset.html");
|
|
|
const verifyEmailTemplatePath = path.join(__dirname, "template-verify.html");
|
|
|
const changeEmailTemplatePath = path.join(__dirname,"template-change-email.html");
|
|
|
-const resetEmailTemplate = fs
|
|
|
- .readFileSync(resetEmailTemplatePath, { encoding: "utf-8" })
|
|
|
- .replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
|
|
|
- .replace(/{{site_name}}/gm, env.SITE_NAME);
|
|
|
-const verifyEmailTemplate = fs
|
|
|
- .readFileSync(verifyEmailTemplatePath, { encoding: "utf-8" })
|
|
|
- .replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
|
|
|
- .replace(/{{site_name}}/gm, env.SITE_NAME);
|
|
|
-const changeEmailTemplate = fs
|
|
|
- .readFileSync(changeEmailTemplatePath, { encoding: "utf-8" })
|
|
|
- .replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
|
|
|
- .replace(/{{site_name}}/gm, env.SITE_NAME);
|
|
|
+
|
|
|
+
|
|
|
+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);
|
|
|
+}
|
|
|
|
|
|
async function verification(user) {
|
|
|
+ if (!env.MAIL_ENABLED) {
|
|
|
+ throw new Error("Attempting to send verification email but email is not enabled.");
|
|
|
+ };
|
|
|
+
|
|
|
const mail = await transporter.sendMail({
|
|
|
from: env.MAIL_FROM || env.MAIL_USER,
|
|
|
to: user.email,
|
|
|
@@ -58,6 +71,10 @@ async function verification(user) {
|
|
|
}
|
|
|
|
|
|
async function changeEmail(user) {
|
|
|
+ if (!env.MAIL_ENABLED) {
|
|
|
+ throw new Error("Attempting to send change email token but email is not enabled.");
|
|
|
+ };
|
|
|
+
|
|
|
const mail = await transporter.sendMail({
|
|
|
from: env.MAIL_FROM || env.MAIL_USER,
|
|
|
to: user.change_email_address,
|
|
|
@@ -78,6 +95,10 @@ async function changeEmail(user) {
|
|
|
}
|
|
|
|
|
|
async function resetPasswordToken(user) {
|
|
|
+ if (!env.MAIL_ENABLED) {
|
|
|
+ throw new Error("Attempting to send reset password email but email is not enabled.");
|
|
|
+ };
|
|
|
+
|
|
|
const mail = await transporter.sendMail({
|
|
|
from: env.MAIL_FROM || env.MAIL_USER,
|
|
|
to: user.email,
|
|
|
@@ -89,7 +110,7 @@ async function resetPasswordToken(user) {
|
|
|
.replace(/{{resetpassword}}/gm, user.reset_password_token)
|
|
|
.replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
if (!mail.accepted.length) {
|
|
|
throw new CustomError(
|
|
|
"Couldn't send reset password email. Try again later."
|
|
|
@@ -98,6 +119,10 @@ async function resetPasswordToken(user) {
|
|
|
}
|
|
|
|
|
|
async function sendReportEmail(link) {
|
|
|
+ if (!env.MAIL_ENABLED) {
|
|
|
+ throw new Error("Attempting to send report email but email is not enabled.");
|
|
|
+ };
|
|
|
+
|
|
|
const mail = await transporter.sendMail({
|
|
|
from: env.MAIL_FROM || env.MAIL_USER,
|
|
|
to: env.REPORT_EMAIL,
|