Skip to content

Commit

Permalink
feat(productiviy-report): email config (#8571)
Browse files Browse the repository at this point in the history
Add ability to customize email headers for non-transactional emails.
  • Loading branch information
Tymek authored Oct 29, 2024
1 parent 9809316 commit 30c14ff
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/lib/__snapshots__/create-config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ exports[`should create default config 1`] = `
"disableScheduler": undefined,
"email": {
"host": undefined,
"optionalHeaders": {},
"port": 587,
"secure": false,
"sender": "Unleash <[email protected]>",
Expand Down
2 changes: 2 additions & 0 deletions src/lib/create-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
} from './types/models/api-token';
import {
parseEnvVarBoolean,
parseEnvVarJSON,
parseEnvVarNumber,
parseEnvVarStrings,
} from './util/parseEnvVar';
Expand Down Expand Up @@ -348,6 +349,7 @@ const defaultEmail: IEmailOption = {
sender: process.env.EMAIL_SENDER || 'Unleash <[email protected]>',
smtpuser: process.env.EMAIL_USER,
smtppass: process.env.EMAIL_PASSWORD,
optionalHeaders: parseEnvVarJSON(process.env.EMAIL_OPTIONAL_HEADERS, {}),
};

const dbPort = (dbConfig: Partial<IDBOption>): Partial<IDBOption> => {
Expand Down
18 changes: 17 additions & 1 deletion src/lib/services/email-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface IEmailEnvelope {
path: string;
cid: string;
}[];
headers?: Record<string, string>;
}

const RESET_MAIL_SUBJECT = 'Unleash - Reset your password';
Expand Down Expand Up @@ -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';
Expand All @@ -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,
Expand All @@ -569,7 +583,9 @@ export class EmailService {
'unleashLogo',
),
],
};
headers,
} satisfies IEmailEnvelope;

process.nextTick(() => {
this.mailer!.sendMail(email).then(
() =>
Expand Down
1 change: 1 addition & 0 deletions src/lib/types/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export interface IEmailOption {
smtpuser?: string;
smtppass?: string;
transportOptions?: SMTPTransport.Options;
optionalHeaders?: Record<string, unknown>;
}

export interface IListeningPipe {
Expand Down
15 changes: 15 additions & 0 deletions src/lib/util/parseEnvVar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;">
Expand Down Expand Up @@ -72,8 +72,9 @@
</div>
<div class="unsubscribe" style="font-size: 12px;color: #888;margin-top: 10px;">
This email was sent to {{userEmail}}. You’ve 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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}

0 comments on commit 30c14ff

Please sign in to comment.