Skip to content

Email for professionals and teams

Add support for HTML signature #43

Hieu Nguyen (Jack)c57671ff9168
diff --git a/src/app/(dashboard)/inbox/[messageId]/email-html-sanitizer-types.d.ts b/src/app/(dashboard)/inbox/[messageId]/email-html-sanitizer-types.d.ts
new file mode 100644
index 0000000..21d7c74
--- /dev/null
+++ b/src/app/(dashboard)/inbox/[messageId]/email-html-sanitizer-types.d.ts
@@ -0,0 +1,3 @@
+export type SanitizeEmailHtmlOptions = {
+ forOutgoing?: boolean;
+};
diff --git a/src/app/(dashboard)/inbox/[messageId]/email-html-sanitizer.ts b/src/app/(dashboard)/inbox/[messageId]/email-html-sanitizer.ts
index ff390fe..b1de5fa 100644
--- a/src/app/(dashboard)/inbox/[messageId]/email-html-sanitizer.ts
+++ b/src/app/(dashboard)/inbox/[messageId]/email-html-sanitizer.ts
@@ -1,3 +1,5 @@
+import type { SanitizeEmailHtmlOptions } from "./email-html-sanitizer-types";
+
const ALLOWED_TAGS = new Set([
"a",
"abbr",
@@ -127,33 +129,33 @@ const ALLOWED_STYLE_PROPERTIES = new Set([
]);
const APP_FONT_FALLBACK = "var(--font-geist-sans), system-ui, sans-serif";
-function isSafeLinkUrl(value: string): boolean {
+function isSafeLinkUrl(value: string, options: SanitizeEmailHtmlOptions): boolean {
try {
- const url = new URL(value, window.location.origin);
+ const url = options.forOutgoing ? new URL(value) : new URL(value, window.location.origin);
return ["http:", "https:", "mailto:", "tel:"].includes(url.protocol);
} catch {
return false;
}
}
-function isSafeImageUrl(value: string): boolean {
- if (value.startsWith("/api/messages/")) return true;
+function isSafeImageUrl(value: string, options: SanitizeEmailHtmlOptions): boolean {
+ if (!options.forOutgoing && value.startsWith("/api/messages/")) return true;
if (/^data:image\/(?:gif|jpeg|png|webp);base64,/i.test(value)) return true;
try {
- const url = new URL(value, window.location.origin);
- return url.protocol === "http:" || url.protocol === "https:";
+ const url = options.forOutgoing ? new URL(value) : new URL(value, window.location.origin);
+ return options.forOutgoing ? url.protocol === "https:" : url.protocol === "http:" || url.protocol === "https:";
} catch {
return false;
}
}
-function sanitizeStyle(element: HTMLElement): void {
+function sanitizeStyle(element: HTMLElement, options: SanitizeEmailHtmlOptions): void {
const safeDeclarations: string[] = [];
for (const property of Array.from(element.style)) {
if (!ALLOWED_STYLE_PROPERTIES.has(property)) continue;
const value = element.style.getPropertyValue(property);
if (/url\s*\(|expression\s*\(|javascript:|@import|behavior\s*:|-moz-binding/i.test(value)) continue;
- const safeValue = property === "font-family"
+ const safeValue = property === "font-family" && !options.forOutgoing
? `${value}, ${APP_FONT_FALLBACK}`
: value;
safeDeclarations.push(`${property}: ${safeValue}`);
@@ -165,7 +167,7 @@ function sanitizeStyle(element: HTMLElement): void {
}
}
-function sanitizeElement(element: Element): void {
+function sanitizeElement(element: Element, options: SanitizeEmailHtmlOptions): void {
const tag = element.tagName.toLowerCase();
if (!ALLOWED_TAGS.has(tag)) {
if (DROP_CONTENT_TAGS.has(tag)) {
@@ -182,11 +184,11 @@ function sanitizeElement(element: Element): void {
if (!allowed || name.startsWith("on")) element.removeAttribute(attribute.name);
}
- if (element instanceof HTMLElement) sanitizeStyle(element);
+ if (element instanceof HTMLElement) sanitizeStyle(element, options);
if (tag === "a") {
const href = element.getAttribute("href");
- if (!href || !isSafeLinkUrl(href)) {
+ if (!href || !isSafeLinkUrl(href, options)) {
element.removeAttribute("href");
} else {
element.setAttribute("target", "_blank");
@@ -196,7 +198,7 @@ function sanitizeElement(element: Element): void {
if (tag === "img") {
const src = element.getAttribute("src");
- if (!src || !isSafeImageUrl(src)) {
+ if (!src || !isSafeImageUrl(src, options)) {
element.remove();
return;
}
@@ -205,7 +207,7 @@ function sanitizeElement(element: Element): void {
}
}
-export function sanitizeEmailHtml(html: string | null): string | null {
+export function sanitizeEmailHtml(html: string | null, options: SanitizeEmailHtmlOptions = {}): string | null {
if (!html) return null;
const document = new DOMParser().parseFromString(html, "text/html");
for (const blockquote of Array.from(document.body.querySelectorAll("blockquote"))) {
@@ -220,7 +222,7 @@ export function sanitizeEmailHtml(html: string | null): string | null {
}
}
for (const element of Array.from(document.body.querySelectorAll("*"))) {
- sanitizeElement(element);
+ sanitizeElement(element, options);
}
return document.body.innerHTML;
}
diff --git a/src/components/compose/rich-text-utils.ts b/src/components/compose/rich-text-utils.ts
index 00f0171..7f07c84 100644
--- a/src/components/compose/rich-text-utils.ts
+++ b/src/components/compose/rich-text-utils.ts
@@ -4,6 +4,8 @@
* a single source of truth for what the message says.
*/
+import { sanitizeEmailHtml } from "@/app/(dashboard)/inbox/[messageId]/email-html-sanitizer";
+
export const QUOTE_ATTRIBUTE = "data-mailflare-quote";
const QUOTE_OPEN = `<div class="mailflare-quote" ${QUOTE_ATTRIBUTE}="1">`;
const SIGNATURE_ATTRIBUTE = "data-mailflare-signature";
@@ -46,9 +48,23 @@ export function hasMeaningfulHtml(html: string): boolean {
return htmlToPlainText(html).trim().length > 0 || /<img\b/i.test(html);
}
-function signatureBlock(signature: string | null | undefined): string {
+/** Distinguish stored HTML markup from existing plain-text signatures. */
+export function isHtmlSignature(signature: string | null | undefined): boolean {
+ return /<\/?[a-z][a-z0-9]*[\s>/]/i.test(signature ?? "");
+}
+
+export function signatureToHtml(signature: string | null | undefined): string {
const value = signature?.trim() ?? "";
- return value ? `<div ${SIGNATURE_ATTRIBUTE}="1"><br><br>${textToHtml(value).replace(/^<div>|<\/div>$/g, "")}</div>` : "";
+ if (!value) return "";
+ if (isHtmlSignature(value) && typeof DOMParser !== "undefined") {
+ return sanitizeEmailHtml(value, { forOutgoing: true }) ?? "";
+ }
+ return textToHtml(value).replace(/^<div>|<\/div>$/g, "");
+}
+
+function signatureBlock(signature: string | null | undefined): string {
+ const html = signatureToHtml(signature);
+ return html ? `<div ${SIGNATURE_ATTRIBUTE}="1"><br><br>${html}</div>` : "";
}
/** Swap or append the mailbox signature, mirroring the plain-text behaviour. */
diff --git a/src/components/settings/mailbox-signature-form.tsx b/src/components/settings/mailbox-signature-form.tsx
index f25b428..6f1e4fb 100644
--- a/src/components/settings/mailbox-signature-form.tsx
+++ b/src/components/settings/mailbox-signature-form.tsx
@@ -2,6 +2,7 @@
import { useEffect, useState } from "react";
import { useSelectedMailbox } from "@/components/mailbox-provider";
+import { isHtmlSignature, signatureToHtml } from "@/components/compose/rich-text-utils";
import { Button } from "@/components/ui/button";
import { Label } from "@/components/ui/label";
import { Textarea } from "@/components/ui/textarea";
@@ -58,9 +59,18 @@ export function MailboxSignatureForm() {
disabled={!canManage || saving}
/>
<p className="text-xs leading-5 text-neutral-500">
- This signature is added when composing from the selected inbox.
+ This signature is added when composing from the selected inbox. Plain text or HTML is supported; HTML is sanitized.
</p>
</div>
+ {isHtmlSignature(signature) && (
+ <div className="space-y-2">
+ <p className="text-xs font-medium text-neutral-500">Preview</p>
+ <div
+ className="rounded-md border border-neutral-200 bg-white p-4 text-sm text-neutral-900"
+ dangerouslySetInnerHTML={{ __html: signatureToHtml(signature) }}
+ />
+ </div>
+ )}
<div className="flex items-center gap-3">
<Button type="submit" disabled={!canManage || saving || signature.trim() === savedSignature}>
{saving ? "Saving..." : "Save signature"}