Contact: mail via achtergrondservice met claim, backoff en maximum (CMS-U5)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-10 15:28:24 +01:00
co-authored by Claude Sonnet 5.5
parent 728fc08db8
commit bc7936034c
28 changed files with 1273 additions and 107 deletions
@@ -101,6 +101,19 @@ public sealed class MailTemplateRenderer : IMailTemplateRenderer
"An administrator has changed your role at {0} to: {1}.",
null, "Think this is a mistake? Please contact the administrator.",
"This message was sent automatically by {0}.")
},
// The details (name, e-mail address, subject, message) come from MailModel.Details; nothing the visitor
// typed is part of the subject, which stays fixed text.
[MailKind.ContactMessage] = new()
{
["nl"] = new("Nieuw bericht via {0}", "Nieuw bericht via het contactformulier",
"Er is een bericht verstuurd via het contactformulier van {0}.",
null, "Je kunt direct op deze mail antwoorden: het antwoord gaat naar het e-mailadres van de afzender.",
"Deze mail is automatisch verstuurd door {0}."),
["en"] = new("New message via {0}", "New message from the contact form",
"A message was sent through the contact form of {0}.",
null, "You can reply to this message directly: the reply goes to the sender's e-mail address.",
"This message was sent automatically by {0}.")
}
};
@@ -135,7 +148,7 @@ public sealed class MailTemplateRenderer : IMailTemplateRenderer
var hasAction = t.Button is not null && !string.IsNullOrEmpty(model.ActionUrl);
return new RenderedMail(subject, BuildHtml(model, site, colour, greeting, heading, intro, hasAction ? t.Button : null, note, footer),
BuildText(greeting, heading, intro, hasAction ? model.ActionUrl : null, note, footer));
BuildText(greeting, heading, intro, hasAction ? model.ActionUrl : null, note, footer, model.Details));
}
private static string RoleName(string lang, string? key) =>
@@ -224,6 +237,7 @@ public sealed class MailTemplateRenderer : IMailTemplateRenderer
sb.Append($"<h1 class=\"mail-accent\" style=\"margin:0 0 16px 0;font-size:22px;color:{colour};\">{E(heading)}</h1>");
if (greeting is not null) sb.Append($"<p style=\"margin:0 0 12px 0;\">{E(greeting)}</p>");
sb.Append($"<p style=\"margin:0 0 24px 0;\">{E(intro)}</p>");
AppendDetails(sb, model.Details);
if (button is not null && !string.IsNullOrEmpty(model.ActionUrl))
{
// 'Bulletproof' button: a table cell with bgcolor + style, a visible border, and the link filling the cell.
@@ -240,12 +254,43 @@ public sealed class MailTemplateRenderer : IMailTemplateRenderer
return sb.ToString();
}
private static string BuildText(string? greeting, string heading, string intro, string? url, string? note, string footer)
/// <summary>
/// Labelled values (label on its own line in the muted colour, value below it). Only classes the dark block already
/// overrides carry an inline colour (.mail-muted, .mail-rule); the values inherit the card's text colour, so they
/// read in light and dark. Every value is HTML-encoded; line breaks in a value become &lt;br&gt;.
/// </summary>
private static void AppendDetails(StringBuilder sb, IReadOnlyList<MailDetail>? details)
{
if (details is null || details.Count == 0) return;
sb.Append("<table role=\"presentation\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" style=\"margin:0 0 24px 0;\">");
foreach (var detail in details)
{
var value = E(detail.Value.Replace("\r\n", "\n").Replace('\r', '\n')).Replace("\n", "<br>");
sb.Append("<tr><td class=\"mail-rule\" style=\"padding:10px 0;border-top:1px solid #e5e7eb;\">");
sb.Append($"<div class=\"mail-muted\" style=\"font-size:12px;color:#6b7280;margin:0 0 2px 0;\">{E(detail.Label)}</div>");
sb.Append($"<div style=\"font-size:15px;line-height:1.5;word-break:break-word;\">{value}</div>");
sb.Append("</td></tr>");
}
sb.Append("</table>");
}
private static string BuildText(
string? greeting, string heading, string intro, string? url, string? note, string footer,
IReadOnlyList<MailDetail>? details = null)
{
var sb = new StringBuilder();
sb.AppendLine(heading).AppendLine();
if (greeting is not null) sb.AppendLine(greeting).AppendLine();
sb.AppendLine(intro).AppendLine();
if (details is { Count: > 0 })
{
foreach (var detail in details)
{
// Plain text: a label line, then the value as written (the message keeps its own line breaks).
sb.Append(detail.Label).AppendLine(":").AppendLine(detail.Value.Replace("\r\n", "\n").Replace('\r', '\n')).AppendLine();
}
}
if (url is not null) sb.AppendLine(url).AppendLine();
if (note is not null) sb.AppendLine(note).AppendLine();
sb.AppendLine("--").AppendLine(footer);
+11 -3
View File
@@ -33,7 +33,9 @@ public sealed record OutgoingMail(
string HtmlBody,
string? TextBody = null,
string? FromAddress = null,
string? FromName = null);
string? FromName = null,
string? ReplyToAddress = null,
string? ReplyToName = null);
public enum MailKind
{
@@ -42,9 +44,14 @@ public enum MailKind
PasswordReset,
AccountDeactivated,
AccountReactivated,
RoleChanged
RoleChanged,
/// <summary>A visitor's message from the contact form, mailed to the site owner (details + Reply-To).</summary>
ContactMessage
}
/// <summary>One labelled value in the details block of a mail (for example the visitor's name). Always encoded in HTML.</summary>
public sealed record MailDetail(string Label, string Value);
/// <summary>Values the template needs. <see cref="Detail"/> carries the role key for <see cref="MailKind.RoleChanged"/>.</summary>
public sealed record MailModel(
string SiteName,
@@ -54,7 +61,8 @@ public sealed record MailModel(
string? Detail = null,
string? LogoUrl = null,
string? BrandColor = null,
string? LogoDarkUrl = null);
string? LogoDarkUrl = null,
IReadOnlyList<MailDetail>? Details = null);
public sealed record RenderedMail(string Subject, string Html, string Text);
@@ -40,6 +40,14 @@ public sealed class SmtpEmailSender(IOptions<EmailOptions> options, ILogger<Smtp
var message = new MimeMessage();
message.From.Add(new MailboxAddress(fromName, fromAddress));
message.To.Add(MailboxAddress.Parse(mail.ToAddress));
// Optional Reply-To (the contact form: the visitor). Same allowlist rule as every other address; a value that
// does not pass is left out rather than failing the mail, and a display name with control characters is dropped.
if (EmailAddressRules.IsValidAddress(mail.ReplyToAddress))
{
var replyName = mail.ReplyToName is { } n && EmailAddressRules.IsValidDisplayName(n) ? n.Trim() : null;
message.ReplyTo.Add(new MailboxAddress(replyName, mail.ReplyToAddress));
}
message.Subject = mail.Subject;
message.Body = new BodyBuilder { HtmlBody = mail.HtmlBody, TextBody = mail.TextBody }.ToMessageBody();