Send text alongside HTML in emails.

This commit is contained in:
zkldi
2021-11-13 09:38:28 +00:00
parent 0a8ea0f202
commit d0a67b932d
4 changed files with 30 additions and 23 deletions
+2 -1
View File
@@ -21,7 +21,8 @@ if (require.main === module) {
await SendEmail(
options.email,
"Hello World",
MainHTMLWrapper("Hello world! This is a test email for doing things.")
MainHTMLWrapper("Hello world! This is a test email for doing things."),
"Hello world! This is a test email for doing things."
);
logger.info(`Done.`);
process.exit(0);
+3 -1
View File
@@ -45,7 +45,8 @@ if (ServerConfig.EMAIL_CONFIG) {
export function SendEmail(
to: string,
subject: string,
htmlContent: string
htmlContent: string,
textContent: string
): Promise<SentMessageInfo> | undefined {
if (Environment.nodeEnv === "test") {
logger.debug(`Stubbed out SendEmail as env was test.`);
@@ -64,6 +65,7 @@ export function SendEmail(
to,
subject,
html: htmlContent,
text: textContent,
dkim: ServerConfig.EMAIL_CONFIG?.DKIM,
});
}
+15 -6
View File
@@ -4,15 +4,24 @@
import { ServerConfig, TachiConfig } from "lib/setup/config";
export function EmailFormatResetPassword(username: string, resetCode: string, ipAddr: string) {
return MainHTMLWrapper(
`Hey ${username}, you've recieved a password reset request.<br/><a href="${ServerConfig.OUR_URL}/reset-password?code=${resetCode}">Click here</a> to perform the reset.<br/>If you did not request this reset, report this! This reset request was made by ${ipAddr}.`
);
return {
html: MainHTMLWrapper(
`Hey ${username}, you've recieved a password reset request.<br/><a href="${ServerConfig.OUR_URL}/reset-password?code=${resetCode}">Click here</a> to perform the reset.<br/>If you did not request this reset, report this! This reset request was made by ${ipAddr}.`
),
text: `Hey ${username}, you've recieved a password reset request. This link
${ServerConfig.OUR_URL}/reset-password?code=${resetCode} will perform the reset.
If you did not request this reset, report this! This reset request was made by ${ipAddr}.`,
};
}
export function EmailFormatVerifyEmail(username: string, code: string) {
return MainHTMLWrapper(
`Hey ${username}, You need to verify your email before you can use the site.<br/><a href="${ServerConfig.OUR_URL}/verify-email?code=${code}">Click here</a> to verify your email.`
);
return {
html: MainHTMLWrapper(
`Hey ${username}, You need to verify your email before you can use the site.<br/><a href="${ServerConfig.OUR_URL}/verify-email?code=${code}">Click here</a> to verify your email.`
),
text: `Hey ${username}, You need to verify your email before you can use the site.
Please go to ${ServerConfig.OUR_URL}/verify-email?code=${code} to verify!`,
};
}
export function MainHTMLWrapper(innerHTML: string) {
+10 -15
View File
@@ -280,11 +280,9 @@ router.post(
email: req.body.email,
});
SendEmail(
req.body.email,
"Email Verification",
EmailFormatVerifyEmail(user!.username, resetEmailCode)
);
const { text, html } = EmailFormatVerifyEmail(user!.username, resetEmailCode);
SendEmail(req.body.email, "Email Verification", html, text);
return res.status(200).json({
success: true,
@@ -387,11 +385,10 @@ router.post("/resend-verify-email", prValidate({ email: "string" }), async (req,
}
// Send the email again.
SendEmail(
req.body.email,
"Email Verification",
EmailFormatVerifyEmail(user!.username, verifyInfo.code)
);
const { text, html } = EmailFormatVerifyEmail(user!.username, verifyInfo.code);
SendEmail(req.body.email, "Email Verification", html, text);
});
/**
@@ -461,11 +458,9 @@ router.post("/forgot-password", prValidate({ email: "string" }), async (req, res
createdOn: Date.now(),
});
SendEmail(
userPrivateInfo.email,
"Reset Password",
EmailFormatResetPassword(user.username, code, req.ip)
);
const { html, text } = EmailFormatResetPassword(user.username, code, req.ip);
SendEmail(userPrivateInfo.email, "Reset Password", html, text);
} else {
logger.info(
`Silently rejected password reset request for ${req.body.email}, as no user has this email.`