Mail: donker logo voor dark mode (Email:LogoDarkUrl) en lichte plaat achter het logo

- Optionele Email:LogoDarkUrl, zelfde validatie als LogoUrl; leeg = geen swap
- Twee img-klassen; dark-blok verbergt het lichte logo (incl. mso-hide) en toont het donkere
- Lichte afgeronde plaat (bgcolor + vlak verloop) achter het logo als vangnet voor clients die media queries negeren of inverteren
- ASF: LogoDarkUrl = /admin/brand-logo-on-dark.png (publiek bereikbaar gecontroleerd); SLP leeg (geen on-dark logo)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 13:21:58 +01:00
co-authored by Claude Sonnet 5.5
parent 21dd386a5d
commit 2cdcb9d868
9 changed files with 78 additions and 3 deletions
@@ -85,5 +85,6 @@ public class HostConfigurationTests
config["Email:Smtp:Password"].Should().BeNullOrEmpty(); config["Email:Smtp:Password"].Should().BeNullOrEmpty();
config["Email:FromAddress"].Should().NotBeNullOrEmpty(); config["Email:FromAddress"].Should().NotBeNullOrEmpty();
config["Email:LogoUrl"].Should().Be("/admin/brand-logo-on-light.png"); config["Email:LogoUrl"].Should().Be("/admin/brand-logo-on-light.png");
config["Email:LogoDarkUrl"].Should().Be("/admin/brand-logo-on-dark.png");
} }
} }
@@ -58,6 +58,7 @@
"FromName": "Andrea Slop Fotografie", "FromName": "Andrea Slop Fotografie",
"AllowedFromAddresses": [], "AllowedFromAddresses": [],
"LogoUrl": "/admin/brand-logo-on-light.png", "LogoUrl": "/admin/brand-logo-on-light.png",
"LogoDarkUrl": "/admin/brand-logo-on-dark.png",
"BrandColor": "#5f6a58", "BrandColor": "#5f6a58",
"Smtp": { "Smtp": {
"Host": "", "Host": "",
@@ -227,6 +227,48 @@ public class MailTemplateRendererTests
html.Should().Contain($".mail-accent{{color:{dark} !important;"); html.Should().Contain($".mail-accent{{color:{dark} !important;");
} }
private const string DarkLogo = "https://t.nl/admin/brand-logo-on-dark.png";
[Fact]
public void WithADarkLogo_BothImagesAreThere_AndDarkModeSwapsThem()
{
var html = _renderer.Render(MailKind.Invitation, "nl", Model() with { LogoDarkUrl = DarkLogo }).Html;
html.Should().Contain("class=\"mail-logo-light\" src=\"https://t.nl/admin/brand-logo-on-light.png\"")
.And.Contain($"class=\"mail-logo-dark\" src=\"{DarkLogo}\"");
// the dark logo is hidden by default (with the Outlook safety net) ...
html.Should().MatchRegex("class=\"mail-logo-dark\"[^>]*display:none;[^>]*mso-hide:all");
// ... and the dark block swaps them and drops the plate
html.Should().Contain(".mail-logo-light{display:none !important").And.Contain(".mail-logo-dark{display:block !important")
.And.Contain(".mail-plate{background:transparent !important");
}
[Fact]
public void WithoutADarkLogo_ThereIsNoSwap_AndTheLightLogoKeepsItsLightPlate()
{
var html = _renderer.Render(MailKind.Invitation, "nl", Model()).Html;
html.Should().NotContain("mail-logo-dark").And.NotContain(".mail-logo-light{").And.NotContain(".mail-plate{");
// the plate survives clients that invert backgrounds or ignore the media query
html.Should().Contain("class=\"mail-plate\" bgcolor=\"#f3f4f6\"").And.Contain("background-image:linear-gradient(#f3f4f6,#f3f4f6)");
}
[Fact]
public void ADarkLogoWithoutALightLogo_IsIgnored_AndTheSiteNameIsShown()
{
var html = _renderer.Render(MailKind.Invitation, "nl", Model() with { LogoUrl = null, LogoDarkUrl = DarkLogo }).Html;
html.Should().NotContain("brand-logo-on-dark").And.NotContain("mail-logo-dark");
}
[Fact]
public void ADarkLogoUrl_IsHtmlEncoded_NotInterpreted()
{
var html = _renderer.Render(MailKind.Invitation, "nl", Model() with { LogoDarkUrl = "https://t.nl/x.png\"><script>alert(1)</script>" }).Html;
html.Should().NotContain("<script>").And.Contain("&lt;script&gt;");
}
[Fact] [Fact]
public void TheSlpRed_GivesAWhiteButtonText() public void TheSlpRed_GivesAWhiteButtonText()
{ {
@@ -116,6 +116,8 @@ public class MailSoftFailureTests
[InlineData("Email:Smtp:TimeoutSeconds", "121", "Email:Smtp:TimeoutSeconds")] [InlineData("Email:Smtp:TimeoutSeconds", "121", "Email:Smtp:TimeoutSeconds")]
[InlineData("Email:Smtp:Password", "", "Email:Smtp:Password")] [InlineData("Email:Smtp:Password", "", "Email:Smtp:Password")]
[InlineData("Email:LogoUrl", "javascript:x", "Email:LogoUrl")] [InlineData("Email:LogoUrl", "javascript:x", "Email:LogoUrl")]
[InlineData("Email:LogoDarkUrl", "javascript:x", "Email:LogoDarkUrl")]
[InlineData("Email:LogoDarkUrl", "//evil.example/x.png", "Email:LogoDarkUrl")]
[InlineData("App:PublicBaseUrl", "", "App:PublicBaseUrl")] [InlineData("App:PublicBaseUrl", "", "App:PublicBaseUrl")]
[InlineData("App:PublicBaseUrl", "http://example.nl", "App:PublicBaseUrl")] [InlineData("App:PublicBaseUrl", "http://example.nl", "App:PublicBaseUrl")]
[InlineData("App:DefaultLanguage", "xx", "App:DefaultLanguage")] [InlineData("App:DefaultLanguage", "xx", "App:DefaultLanguage")]
@@ -31,6 +31,12 @@ public sealed class EmailOptions
/// <summary>Optional logo for the mail header, absolute or relative to <c>App:PublicBaseUrl</c>.</summary> /// <summary>Optional logo for the mail header, absolute or relative to <c>App:PublicBaseUrl</c>.</summary>
public string LogoUrl { get; set; } = ""; public string LogoUrl { get; set; } = "";
/// <summary>
/// Optional logo variant for dark mode (same rules as <see cref="LogoUrl"/>). Empty = no swap: the light logo then
/// stays on its light plate.
/// </summary>
public string LogoDarkUrl { get; set; } = "";
/// <summary>Optional brand colour, <c>#rrggbb</c>.</summary> /// <summary>Optional brand colour, <c>#rrggbb</c>.</summary>
public string BrandColor { get; set; } = ""; public string BrandColor { get; set; } = "";
@@ -52,6 +52,8 @@ public sealed class EmailOptionsValidator(IOptions<AppMailOptions> app) : IValid
if (!string.IsNullOrEmpty(options.LogoUrl) && !IsValidLogoUrl(options.LogoUrl)) if (!string.IsNullOrEmpty(options.LogoUrl) && !IsValidLogoUrl(options.LogoUrl))
failures.Add("Email:LogoUrl must be an https URL or a path starting with '/'."); failures.Add("Email:LogoUrl must be an https URL or a path starting with '/'.");
if (!string.IsNullOrEmpty(options.LogoDarkUrl) && !IsValidLogoUrl(options.LogoDarkUrl))
failures.Add("Email:LogoDarkUrl must be an https URL or a path starting with '/'.");
return failures; return failures;
} }
@@ -84,7 +84,8 @@ public sealed class IdentityMailService(
ValidForMinutes: validForMinutes, ValidForMinutes: validForMinutes,
Detail: detail, Detail: detail,
LogoUrl: ResolveLogo(baseUrl, email.Value.LogoUrl), LogoUrl: ResolveLogo(baseUrl, email.Value.LogoUrl),
BrandColor: string.IsNullOrWhiteSpace(email.Value.BrandColor) ? null : email.Value.BrandColor); BrandColor: string.IsNullOrWhiteSpace(email.Value.BrandColor) ? null : email.Value.BrandColor,
LogoDarkUrl: ResolveLogo(baseUrl, email.Value.LogoDarkUrl));
var rendered = renderer.Render(kind, languages.Resolve(language), model); var rendered = renderer.Render(kind, languages.Resolve(language), model);
var result = await sender.SendAsync( var result = await sender.SendAsync(
@@ -160,6 +160,7 @@ public sealed class MailTemplateRenderer : IMailTemplateRenderer
private const string DarkPage = "#111827"; private const string DarkPage = "#111827";
private const string DarkCard = "#1f2937"; private const string DarkCard = "#1f2937";
private const string PlateColour = "#f3f4f6";
private static string BuildHtml( private static string BuildHtml(
MailModel model, string site, string colour, string? greeting, string heading, string intro, MailModel model, string site, string colour, string? greeting, string heading, string intro,
@@ -169,6 +170,7 @@ public sealed class MailTemplateRenderer : IMailTemplateRenderer
// brand colour (readable on the dark card) with its own best-contrasting text. Both are >= 4.5:1. // brand colour (readable on the dark card) with its own best-contrasting text. Both are >= 4.5:1.
var buttonText = MailColours.ReadableText(colour); var buttonText = MailColours.ReadableText(colour);
var dark = MailColours.DarkAccent(colour, DarkCard); var dark = MailColours.DarkAccent(colour, DarkCard);
var hasDarkLogo = !string.IsNullOrEmpty(model.LogoUrl) && !string.IsNullOrEmpty(model.LogoDarkUrl);
var darkText = MailColours.ReadableText(dark); var darkText = MailColours.ReadableText(dark);
var sb = new StringBuilder(); var sb = new StringBuilder();
@@ -183,13 +185,30 @@ public sealed class MailTemplateRenderer : IMailTemplateRenderer
sb.Append($".mail-btn{{background:{dark} !important;border-color:{dark} !important;}}"); sb.Append($".mail-btn{{background:{dark} !important;border-color:{dark} !important;}}");
sb.Append($".mail-btn a{{background:{dark} !important;color:{darkText} !important;border-color:{darkText} !important;}}"); sb.Append($".mail-btn a{{background:{dark} !important;color:{darkText} !important;border-color:{darkText} !important;}}");
sb.Append(".mail-muted{color:#9ca3af !important;}.mail-rule{border-color:#374151 !important;}"); sb.Append(".mail-muted{color:#9ca3af !important;}.mail-rule{border-color:#374151 !important;}");
if (hasDarkLogo)
{
// Swap: hide the light logo and its plate, show the dark variant.
sb.Append(".mail-logo-light{display:none !important;max-height:0 !important;overflow:hidden !important;mso-hide:all !important;}");
sb.Append(".mail-logo-dark{display:block !important;max-height:none !important;overflow:visible !important;mso-hide:none !important;}");
sb.Append(".mail-plate{background:transparent !important;background-image:none !important;padding:0 !important;}");
}
sb.Append("}</style></head>"); sb.Append("}</style></head>");
sb.Append("<body class=\"mail-page\" style=\"margin:0;padding:0;background:#f3f4f6;\">"); sb.Append("<body class=\"mail-page\" style=\"margin:0;padding:0;background:#f3f4f6;\">");
sb.Append("<table role=\"presentation\" class=\"mail-page\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#f3f4f6\" style=\"background:#f3f4f6;padding:24px 0;\"><tr><td align=\"center\">"); sb.Append("<table role=\"presentation\" class=\"mail-page\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#f3f4f6\" style=\"background:#f3f4f6;padding:24px 0;\"><tr><td align=\"center\">");
sb.Append("<table role=\"presentation\" class=\"mail-card\" width=\"560\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#ffffff\" style=\"max-width:560px;width:100%;background:#ffffff;border-radius:8px;font-family:Arial,Helvetica,sans-serif;color:#111827;\">"); sb.Append("<table role=\"presentation\" class=\"mail-card\" width=\"560\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#ffffff\" style=\"max-width:560px;width:100%;background:#ffffff;border-radius:8px;font-family:Arial,Helvetica,sans-serif;color:#111827;\">");
sb.Append($"<tr><td class=\"mail-accent\" style=\"padding:24px 32px;border-bottom:3px solid {colour};\">"); sb.Append($"<tr><td class=\"mail-accent\" style=\"padding:24px 32px;border-bottom:3px solid {colour};\">");
if (!string.IsNullOrEmpty(model.LogoUrl)) if (!string.IsNullOrEmpty(model.LogoUrl))
sb.Append($"<img src=\"{E(model.LogoUrl)}\" alt=\"{E(site)}\" height=\"48\" style=\"display:block;height:48px;width:auto;border:0;\">"); {
// The light-background logo sits on a light plate (bgcolor + a flat gradient image, which clients that
// invert backgrounds, like the Gmail and Outlook apps, leave alone), so it stays readable even when the
// client ignores the dark-mode rules. With a dark variant, dark mode swaps the two and drops the plate.
sb.Append("<table role=\"presentation\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
sb.Append($"<td class=\"mail-plate\" bgcolor=\"{PlateColour}\" style=\"background-color:{PlateColour};background-image:linear-gradient({PlateColour},{PlateColour});border-radius:8px;padding:8px 12px;\">");
sb.Append($"<img class=\"mail-logo-light\" src=\"{E(model.LogoUrl)}\" alt=\"{E(site)}\" height=\"48\" style=\"display:block;height:48px;width:auto;border:0;\">");
if (hasDarkLogo)
sb.Append($"<img class=\"mail-logo-dark\" src=\"{E(model.LogoDarkUrl!)}\" alt=\"{E(site)}\" height=\"48\" style=\"display:none;max-height:0;overflow:hidden;mso-hide:all;height:48px;width:auto;border:0;\">");
sb.Append("</td></tr></table>");
}
else else
sb.Append($"<span class=\"mail-accent\" style=\"font-size:20px;font-weight:bold;color:{colour};\">{E(site)}</span>"); sb.Append($"<span class=\"mail-accent\" style=\"font-size:20px;font-weight:bold;color:{colour};\">{E(site)}</span>");
sb.Append("</td></tr><tr><td style=\"padding:32px;font-size:15px;line-height:1.5;\">"); sb.Append("</td></tr><tr><td style=\"padding:32px;font-size:15px;line-height:1.5;\">");
+2 -1
View File
@@ -53,7 +53,8 @@ public sealed record MailModel(
int? ValidForMinutes = null, int? ValidForMinutes = null,
string? Detail = null, string? Detail = null,
string? LogoUrl = null, string? LogoUrl = null,
string? BrandColor = null); string? BrandColor = null,
string? LogoDarkUrl = null);
public sealed record RenderedMail(string Subject, string Html, string Text); public sealed record RenderedMail(string Subject, string Html, string Text);