Files
wekan/models/lib/accountEmailHtml.js
T
Lauri Ojansivu 058677e078 E-mails in the clear HTML layout by default: notifications and account mail.
Notification e-mail now defaults to the clearly arranged layout of #5171 -
board first, board, actor and card in bold and linked, with a plain-text
alternative. One sentence (classic) is still one choice away at the
Admin Panel, board and member levels, and the tray keeps the classic line.

Account e-mails - reset password, verify e-mail, enroll account, and the
board and registration invitations - get an HTML part in the same style,
built from the same translated text, which is still sent as the text
part: no translation changes. Every piece of text is escaped first, the
link paragraph becomes a button, and only an http(s) address is linked. A
caller's own HTML and texts that are not WeKan's e-mail strings are left
as they were, and a failure to build the HTML leaves the text alone.

models/lib/accountEmailHtml.js renders it; tests/accountEmailHtml.test.cjs
lays out the real English texts and checks that markup in a name, a board
title or the site name stays text and only http(s) is linked.
notificationDelivery's default assertions now expect clear for e-mail,
and emailLocalizationPreparation's harness allows the new module, checks
the HTML part, and checks the absent-HTML case with literal text.

Thanks to Meeques and xet7 !

Fixes #4434.
2026-10-14 14:33:10 +03:00

53 lines
2.4 KiB
JavaScript

'use strict';
// #4434: account e-mails - reset password, verify e-mail, enroll account and
// board invitations - in the same clear HTML layout notifications use by
// default, beside the plain text they always had (sent as the text part).
//
// The HTML is built from the SAME localized text, so no translation changes:
// each blank-line-separated paragraph becomes a <p>, and a paragraph that is
// the link becomes a button. Every piece of text is escaped first; the only
// markup is the layout's own, and only an http(s) address becomes a link.
// Pure: tests/accountEmailHtml.test.cjs.
const { escapeEmailHtml } = require('./emailNotificationSafety');
const URL_PATTERN = /https?:\/\/[^\s<>"']+/g;
function isHttpUrl(value) {
try {
const u = new URL(value);
return u.protocol === 'http:' || u.protocol === 'https:';
} catch (e) {
return false;
}
}
function linkify(escapedLine) {
// Runs on ESCAPED text: an address can no longer contain < > " or ', so
// what is matched is placed in the href and the link text as it is.
return escapedLine.replace(URL_PATTERN, match => {
const raw = match.replaceAll('&amp;', '&');
return isHttpUrl(raw) ? `<a href="${match}" style="color:#0b6bcb">${match}</a>` : match;
});
}
function renderAccountEmailHtml({ text, siteName } = {}) {
const paragraphs = String(text ?? '').replace(/\r\n/g, '\n').split(/\n{2,}/).map(p => p.trim()).filter(Boolean);
const body = paragraphs.map(paragraph => {
if (isHttpUrl(paragraph) && !/\s/.test(paragraph)) {
const href = escapeEmailHtml(paragraph);
return `<p style="margin:24px 0"><a href="${href}" style="display:inline-block;padding:12px 20px;background:#0b6bcb;color:#ffffff;text-decoration:none;border-radius:6px;font-weight:bold;word-break:break-all">${href}</a></p>`;
}
const lines = paragraph.split('\n').map(line => linkify(escapeEmailHtml(line)));
return `<p style="margin:0 0 16px 0">${lines.join('<br/>\n')}</p>`;
}).join('\n');
const header = siteName
? `<div style="font-size:18px;font-weight:bold;margin:0 0 20px 0">${escapeEmailHtml(siteName)}</div>\n`
: '';
return '<div style="font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;font-size:15px;line-height:1.5;color:#172b4d;max-width:600px;margin:0 auto;padding:24px">\n'
+ `${header}${body}\n</div>`;
}
module.exports = { renderAccountEmailHtml, isHttpUrl };