Halve of foute mailconfiguratie: host start, mail uit, een duidelijke Error-logregel

Eén bron (IMailAvailability) bepaalt of mail aan staat: SMTP-host gezet EN de hele mailconfiguratie geldig.
Anders start de host gewoon, gebruikt de Null-verzender, meldt emailEnabled=false en logt eenmalig
mail_configuration_invalid met de namen van de ontbrekende of ongeldige instellingen (nooit waarden of
geheimen). Getest: halve config, elke soort fout, geen geheim in het log, volledige config werkt als
voorheen, geen mailinstelling geeft geen enkele waarschuwing, alle verbruikers zien mail als uit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-08 20:51:41 +01:00
co-authored by Claude Sonnet 5.5
parent 142815a3ab
commit bd9790819d
12 changed files with 308 additions and 40 deletions
@@ -0,0 +1,62 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace SlpModularCms.Core.Email;
/// <summary>
/// Whether this host can send mail. THE single answer: the sender, the mail service, the site settings and
/// the capabilities endpoint all ask this, so they can never disagree.
/// </summary>
public interface IMailAvailability
{
/// <summary>True only when an SMTP host is set AND the whole mail configuration is valid.</summary>
bool IsEnabled { get; }
/// <summary>Why mail is off although a host was set (setting names only, never values). Empty otherwise.</summary>
IReadOnlyList<string> Problems { get; }
}
/// <summary>
/// Decides once, at start-up. A host without mail settings simply has mail off. A host with a half-finished or
/// wrong mail configuration does NOT fail to start (mail is secondary to the site being up): it starts with mail
/// off and one clear Error log line naming each missing or invalid setting. No secret or value is ever logged.
/// </summary>
public sealed class MailAvailability : IMailAvailability
{
public bool IsEnabled { get; }
public IReadOnlyList<string> Problems { get; }
public MailAvailability(IOptions<EmailOptions> email, IOptions<AppMailOptions> app, ILogger<MailAvailability> logger)
{
if (string.IsNullOrWhiteSpace(email.Value.Smtp.Host))
{
Problems = [];
IsEnabled = false;
return;
}
Problems = EmailOptionsValidator.Check(email.Value, app.Value);
IsEnabled = Problems.Count == 0;
if (!IsEnabled)
{
// The messages name settings (for example "App:PublicBaseUrl"), never their values.
logger.LogError(
"mail_configuration_invalid: an SMTP host is set but the mail configuration is incomplete or invalid, so mail stays OFF and the site runs without it. Fix these settings and restart: {Problems}",
string.Join(" | ", Problems));
}
}
}
/// <summary>Makes the decision (and its Error log line) happen at start-up instead of at the first mail.</summary>
public sealed class MailStartupCheck(IMailAvailability availability, ILogger<MailStartupCheck> logger) : IHostedService
{
public Task StartAsync(CancellationToken cancellationToken)
{
logger.LogInformation("mail_status enabled={Enabled}", availability.IsEnabled);
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}