Files
slp-modular-cms/src/SlpModularCms.Core/Email/MailAvailability.cs
T
SluijsensandClaude Sonnet 5.5 daaadcbdf7 Mail: diagnose van SMTP-weigeringen en keuze bij een nieuwe uitnodigingslink
SMTP: bij een geweigerd commando logt de verzender fase, statuscode en de eerste regel van het serverantwoord
(adressen vervangen, max 120 tekens); specifiekere foutcodes (rejected_sender, rejected_recipient, tls_failed,
connection_failed) die de beheerder in het venster ziet; opstartlog met SMTP-host, poort en standaard
afzenderdomein. Uit de property-test: besturingstekens worden eerst verwijderd, anders kon een adres weer
aan elkaar plakken.
Nieuwe uitnodigingslink: regenerate-invite mailt alleen met sendEmail=true (standaard false); eerst een
keuzevenster 'Alleen link kopieren' / 'Link kopieren en mailen' (zonder mail alleen de link).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-08 22:30:05 +01:00

76 lines
3.2 KiB
C#

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, IOptions<EmailOptions> email, ILogger<MailStartupCheck> logger) : IHostedService
{
public Task StartAsync(CancellationToken cancellationToken)
{
var o = email.Value;
if (availability.IsEnabled)
{
// Where mail goes through and with which default sender DOMAIN (not the address): what a refused
// MAIL FROM is checked against. A sender chosen in the site settings can differ from this default.
var at = o.FromAddress.LastIndexOf('@');
logger.LogInformation(
"mail_status enabled=true smtp_host={Host} smtp_port={Port} starttls={StartTls} default_from_domain={FromDomain}",
o.Smtp.Host, o.Smtp.Port, o.Smtp.EnableSsl, at >= 0 ? o.FromAddress[(at + 1)..] : "?");
}
else
{
logger.LogInformation("mail_status enabled=false");
}
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}