1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

feat(productiviy-report): email config (#8571)

Add ability to customize email headers for non-transactional emails.
This commit is contained in:
Tymoteusz Czech 2024-10-29 10:52:05 +01:00 committed by GitHub
parent 9809316a65
commit 30c14ff995
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 45 additions and 4 deletions

View File

@ -53,6 +53,7 @@ exports[`should create default config 1`] = `
"disableScheduler": undefined,
"email": {
"host": undefined,
"optionalHeaders": {},
"port": 587,
"secure": false,
"sender": "Unleash <noreply@getunleash.io>",

View File

@ -39,6 +39,7 @@ import {
} from './types/models/api-token';
import {
parseEnvVarBoolean,
parseEnvVarJSON,
parseEnvVarNumber,
parseEnvVarStrings,
} from './util/parseEnvVar';
@ -348,6 +349,7 @@ const defaultEmail: IEmailOption = {
sender: process.env.EMAIL_SENDER || 'Unleash <noreply@getunleash.io>',
smtpuser: process.env.EMAIL_USER,
smtppass: process.env.EMAIL_PASSWORD,
optionalHeaders: parseEnvVarJSON(process.env.EMAIL_OPTIONAL_HEADERS, {}),
};
const dbPort = (dbConfig: Partial<IDBOption>): Partial<IDBOption> => {

View File

@ -33,6 +33,7 @@ export interface IEmailEnvelope {
path: string;
cid: string;
}[];
headers?: Record<string, string>;
}
const RESET_MAIL_SUBJECT = 'Unleash - Reset your password';
@ -536,11 +537,14 @@ export class EmailService {
},
): Promise<IEmailEnvelope> {
if (this.configured()) {
const unsubscribeUrl = '{{amazonSESUnsubscribeUrl}}'; // FIXME: Add unsubscribe URL
const context = {
userName,
userEmail,
...metrics,
unleashUrl: this.config.server.unleashUrl,
unsubscribeUrl,
};
const template = 'productivity-report';
@ -555,6 +559,16 @@ export class EmailService {
TemplateFormat.PLAIN,
context,
);
const headers: Record<string, string> = {};
Object.entries(this.config.email.optionalHeaders || {}).forEach(
([key, value]) => {
if (typeof value === 'string') {
headers[key] = value;
}
},
);
const email: IEmailEnvelope = {
from: this.sender,
to: userEmail,
@ -569,7 +583,9 @@ export class EmailService {
'unleashLogo',
),
],
};
headers,
} satisfies IEmailEnvelope;
process.nextTick(() => {
this.mailer!.sendMail(email).then(
() =>

View File

@ -167,6 +167,7 @@ export interface IEmailOption {
smtpuser?: string;
smtppass?: string;
transportOptions?: SMTPTransport.Options;
optionalHeaders?: Record<string, unknown>;
}
export interface IListeningPipe {

View File

@ -38,3 +38,18 @@ export function parseEnvVarStrings(
return defaultVal;
}
export function parseEnvVarJSON(
envVar: string | undefined,
defaultVal: Record<string, unknown>,
): Record<string, unknown> {
if (envVar) {
try {
return JSON.parse(envVar);
} catch (e) {
return defaultVal;
}
}
return defaultVal;
}

View File

@ -6,7 +6,7 @@
<title>Your Unleash Productivity Report</title>
</head>
<body style="font-family: Arial, sans-serif;background-color: #f9f9f9;color: #333;margin: 0;padding: 0;font-size:16px">
<body style="font-family: Arial, sans-serif;background-color: #f9f9f9;color: #333;margin: 0;padding: 12px 0;font-size:16px">
<div class="container"
style="max-width: 600px;margin: 24px auto;background-color: #ffffff;border-radius: 8px;border: 1px solid #f0f0f5;overflow: hidden;">
<div class="header" style="background-color: #6c65e5;padding: 20px;text-align: center;color: #ffffff;">
@ -72,8 +72,9 @@
</div>
<div class="unsubscribe" style="font-size: 12px;color: #888;margin-top: 10px;">
This email was sent to {{userEmail}}. Youve received this as you are a user of Unleash.<br>
If you wish to unsubscribe from our newsletter, click <a
href="https://example.com/unsubscribe">here</a>.
{{#unsubscribeUrl}}
If you wish to unsubscribe from updated, click <a href="{{unsubscribeUrl}}">here</a>.
{{/unsubscribeUrl}}
</div>
</div>
</div>

View File

@ -3,3 +3,8 @@ Subject: Unleash productivity report
Hello,
Productivity report
{{! FIXME: create plaintext template }}
{{#unsubscribeUrl}}
If you wish to unsubscribe from updated, open {{unsubscribeUrl}}.
{{/unsubscribeUrl}}