Skip to content

Commit 97683b9

Browse files
committed
template email notifications with handlebars
1 parent 481e701 commit 97683b9

File tree

5 files changed

+331
-7
lines changed

5 files changed

+331
-7
lines changed

src/api/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"fastify-plugin": "^5.0.1",
4545
"fastify-raw-body": "^5.0.0",
4646
"fastify-zod-openapi": "^4.1.1",
47+
"handlebars": "^4.7.8",
4748
"ical-generator": "^8.1.1",
4849
"ioredis": "^5.6.1",
4950
"jsonwebtoken": "^9.0.2",

src/api/routes/stripe.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ const stripeRoutes: FastifyPluginAsync = async (fastify, _options) => {
296296
payload: {
297297
to: [unmarshalledEntry.userId],
298298
subject: `Payment Recieved for Invoice ${unmarshalledEntry.invoiceId}`,
299-
content: `We're writing to notify you that ACM has received ${paidInFull ? "full" : "partial"} payment for Invoice ${unmarshalledEntry.invoiceId} (${withCurrency} by ${name} <${email}>).\n\nPlease contact Officer Board with any questions.`,
299+
content: `We're writing to notify you that ACM @ UIUC has received ${paidInFull ? "full" : "partial"} payment for Invoice ${unmarshalledEntry.invoiceId} (${withCurrency} by ${name} <${email}>).\n\nPlease contact Officer Board with any questions.`,
300300
},
301301
};
302302
if (!fastify.sqsClient) {

src/api/sqs/handlers/emailNotifications.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ import { SendEmailCommand, SESClient } from "@aws-sdk/client-ses";
44
import { genericConfig } from "common/config.js";
55
import { createAuditLogEntry } from "api/functions/auditLog.js";
66
import { Modules } from "common/modules.js";
7+
import Handlebars from "handlebars";
8+
import emailTemplate from "./templates/notification.js";
9+
10+
Handlebars.registerHelper("nl2br", (text) => {
11+
let nl2br = `${text}`.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, "$1<br>$2");
12+
nl2br = `<p>${nl2br.replace(/<br>/g, "</p><p>")}</p>`;
13+
return new Handlebars.SafeString(nl2br);
14+
});
15+
16+
const compiledTemplate = Handlebars.compile(emailTemplate);
717

818
const stripHtml = (html: string): string => {
919
return html
@@ -33,7 +43,7 @@ export const emailNotificationsHandler: SQSHandlerFunction<
3343
},
3444
Body: {
3545
Html: {
36-
Data: content,
46+
Data: compiledTemplate(payload),
3747
Charset: "UTF-8",
3848
},
3949
Text: {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const template = /*html*/ `
2+
<!doctype html>
3+
<html>
4+
<head>
5+
<title>{{subject}}</title>
6+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
7+
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
8+
<base target="_blank">
9+
<style>
10+
body {
11+
background-color: #F0F1F3;
12+
font-family: 'Helvetica Neue', 'Segoe UI', Helvetica, sans-serif;
13+
font-size: 15px;
14+
line-height: 26px;
15+
margin: 0;
16+
color: #444;
17+
}
18+
.wrap {
19+
background-color: #fff;
20+
padding: 30px;
21+
max-width: 525px;
22+
margin: 0 auto;
23+
border-radius: 5px;
24+
}
25+
.button {
26+
background: #0055d4;
27+
border-radius: 3px;
28+
text-decoration: none !important;
29+
color: #fff !important;
30+
font-weight: bold;
31+
padding: 10px 30px;
32+
display: inline-block;
33+
}
34+
.button:hover {
35+
background: #111;
36+
}
37+
.footer {
38+
text-align: center;
39+
font-size: 12px;
40+
color: #888;
41+
}
42+
img {
43+
max-width: 100%;
44+
height: auto;
45+
}
46+
a {
47+
color: #0055d4;
48+
}
49+
a:hover {
50+
color: #111;
51+
}
52+
@media screen and (max-width: 600px) {
53+
.wrap {
54+
max-width: auto;
55+
}
56+
}
57+
</style>
58+
</head>
59+
<body>
60+
<div class="gutter" style="padding: 30px;">&nbsp;</div>
61+
<img src="https://static.acm.illinois.edu/banner-blue.png" style="height: 100px; width: 210px; align-self: center;"/>
62+
<br />
63+
<div class="wrap">
64+
{{nl2br content}}
65+
</div>
66+
<div class="footer">
67+
<p>
68+
<a href="https://acm.illinois.edu">ACM @ UIUC Homepage</a>
69+
<a href="mailto:[email protected]">Email ACM @ UIUC</a>
70+
</p>
71+
</div>
72+
</body>
73+
</html>
74+
`;
75+
export default template;

0 commit comments

Comments
 (0)