misc: added email self-host conditionals

This commit is contained in:
Sheen Capadngan
2024-07-24 00:55:02 +08:00
parent 02eea4d886
commit 28087cdcc4
4 changed files with 20 additions and 17 deletions
+5 -2
View File
@@ -5,6 +5,7 @@ import handlebars from "handlebars";
import { createTransport } from "nodemailer"; import { createTransport } from "nodemailer";
import SMTPTransport from "nodemailer/lib/smtp-transport"; import SMTPTransport from "nodemailer/lib/smtp-transport";
import { getConfig } from "@app/lib/config/env";
import { logger } from "@app/lib/logger"; import { logger } from "@app/lib/logger";
export type TSmtpConfig = SMTPTransport.Options; export type TSmtpConfig = SMTPTransport.Options;
@@ -12,7 +13,7 @@ export type TSmtpSendMail = {
template: SmtpTemplates; template: SmtpTemplates;
subjectLine: string; subjectLine: string;
recipients: string[]; recipients: string[];
substitutions: unknown; substitutions: object;
}; };
export type TSmtpService = ReturnType<typeof smtpServiceFactory>; export type TSmtpService = ReturnType<typeof smtpServiceFactory>;
@@ -47,9 +48,11 @@ export const smtpServiceFactory = (cfg: TSmtpConfig) => {
const isSmtpOn = Boolean(cfg.host); const isSmtpOn = Boolean(cfg.host);
const sendMail = async ({ substitutions, recipients, template, subjectLine }: TSmtpSendMail) => { const sendMail = async ({ substitutions, recipients, template, subjectLine }: TSmtpSendMail) => {
const appCfg = getConfig();
const html = await fs.readFile(path.resolve(__dirname, "./templates/", template), "utf8"); const html = await fs.readFile(path.resolve(__dirname, "./templates/", template), "utf8");
const temp = handlebars.compile(html); const temp = handlebars.compile(html);
const htmlToSend = temp(substitutions); const htmlToSend = temp({ ...substitutions, isCloud: appCfg.isCloud });
if (isSmtpOn) { if (isSmtpOn) {
await smtp.sendMail({ await smtp.sendMail({
from: cfg.from, from: cfg.from,
@@ -1,19 +1,19 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge"> <meta http-equiv="x-ua-compatible" content="ie=edge" />
<title>MFA Code</title> <title>MFA Code</title>
</head> </head>
<body> <body>
<h2>Infisical</h2> <h2>Infisical</h2>
<h2>Sign in attempt requires further verification</h2> <h2>Sign in attempt requires further verification</h2>
<p>Your MFA code is below — enter it where you started signing in to Infisical.</p> <p>Your MFA code is below — enter it where you started signing in to Infisical.</p>
<h2>{{code}}</h2> <h2>{{code}}</h2>
<p>The MFA code will be valid for 2 minutes.</p> <p>The MFA code will be valid for 2 minutes.</p>
<p>Not you? Contact Infisical or your administrator immediately.</p> <p>Not you? Contact {{#if isCloud}}Infisical{{else}}your administrator{{/if}} immediately.</p>
</body> </body>
</html> </html>
@@ -1,19 +1,19 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge"> <meta http-equiv="x-ua-compatible" content="ie=edge" />
<title>Successful login for {{email}} from new device</title> <title>Successful login for {{email}} from new device</title>
</head> </head>
<body> <body>
<h2>Infisical</h2> <h2>Infisical</h2>
<p>We're verifying a recent login for {{email}}:</p> <p>We're verifying a recent login for {{email}}:</p>
<p><strong>Timestamp</strong>: {{timestamp}}</p> <p><strong>Timestamp</strong>: {{timestamp}}</p>
<p><strong>IP address</strong>: {{ip}}</p> <p><strong>IP address</strong>: {{ip}}</p>
<p><strong>User agent</strong>: {{userAgent}}</p> <p><strong>User agent</strong>: {{userAgent}}</p>
<p>If you believe that this login is suspicious, please contact Infisical or reset your password immediately.</p> <p>If you believe that this login is suspicious, please contact {{#if isCloud}}Infisical{{else}}your administrator{{/if}} or reset your password immediately.</p>
</body> </body>
</html> </html>
@@ -9,6 +9,6 @@
<h2>Reset your password</h2> <h2>Reset your password</h2>
<p>Someone requested a password reset.</p> <p>Someone requested a password reset.</p>
<a href="{{callback_url}}?token={{token}}&to={{email}}">Reset password</a> <a href="{{callback_url}}?token={{token}}&to={{email}}">Reset password</a>
<p>If you didn't initiate this request, please contact us immediately at [email protected]</p> <p>If you didn't initiate this request, please contact {{#if isCloud}}us immediately at [email protected].{{else}}your administrator immediately.{{/if}}</p>
</body> </body>
</html> </html>