using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using SlpModularCms.Core.Email; using SlpModularCms.Core.SiteSettings; using SlpModularCms.Core.Texts; using SlpModularCms.Modules.Contact.Config; using SlpModularCms.Modules.Contact.Data.Entities; namespace SlpModularCms.Modules.Contact.Services; /// /// Mails the notification of a stored message to the site owner, using the host's own mail /// pieces (sender, renderer, effective site sender). The owner can simply reply: the visitor is /// the Reply-To. Nothing the visitor typed ends up in the subject or in any header except /// Reply-To, and no address, name or text is ever logged: only the message id and a short code. /// public sealed class ContactMailSender( IEmailSender sender, IMailTemplateRenderer renderer, ILanguageResolver languages, ISiteSettingsService siteSettings, IContactSettingsService contactSettings, IMailAvailability availability, IOptions email, IOptions app, IOptions contact, ILogger logger, ITextCatalog? texts = null) : IContactMailSender { public async Task NotReadyReasonAsync(CancellationToken cancellationToken) { if (!availability.IsEnabled) { return ContactMailResult.Disabled; } var (address, _) = await contactSettings.GetEffectiveRecipientAsync(cancellationToken); return address is null ? ContactMailResult.RecipientNotConfigured : null; } public async Task SendAsync(ContactMessage message, CancellationToken cancellationToken) { var notReady = await NotReadyReasonAsync(cancellationToken); if (notReady is not null) { return new ContactMailResult(ContactMailOutcome.Skipped, notReady); } try { // Read per send: the administrator's setting wins over the environment and changes without a restart. var (recipient, _) = await contactSettings.GetEffectiveRecipientAsync(cancellationToken); var (fromAddress, fromName) = await siteSettings.GetEffectiveSenderAsync(cancellationToken); var baseUrl = app.Value.PublicBaseUrl.TrimEnd('/'); var language = languages.Resolve(null); var text = texts ?? TextCatalog.Default; var model = new MailModel( SiteName: fromName, LogoUrl: ResolveLogo(baseUrl, email.Value.LogoUrl), BrandColor: string.IsNullOrWhiteSpace(email.Value.BrandColor) ? null : email.Value.BrandColor, LogoDarkUrl: ResolveLogo(baseUrl, email.Value.LogoDarkUrl), Details: [ new MailDetail(text.Get(ContactTexts.MailLabel.Name, language), message.Name), new MailDetail(text.Get(ContactTexts.MailLabel.Email, language), message.Email), new MailDetail(text.Get(ContactTexts.MailLabel.Subject, language), message.Subject), new MailDetail(text.Get(ContactTexts.MailLabel.Message, language), message.Message), ]); var rendered = renderer.Render(MailKind.ContactMessage, language, model); var result = await sender.SendAsync( new OutgoingMail( recipient!, rendered.Subject, rendered.Html, rendered.Text, fromAddress, fromName, ReplyToAddress: message.Email, ReplyToName: EmailAddressRules.IsValidDisplayName(message.Name) ? message.Name : null), cancellationToken); if (result.Sent) { logger.LogInformation("contact_mail_sent message={MessageId}", message.Id); return ContactMailOutcome.Sent; } logger.LogWarning("contact_mail_failed message={MessageId} code={Code}", message.Id, result.ErrorCode); return new ContactMailResult(ContactMailOutcome.Failed, result.ErrorCode ?? MailSendResult.Unknown); } catch (Exception ex) when (ex is not OperationCanceledException) { // Type only: the exception text could quote addresses or content. logger.LogWarning("contact_mail_failed message={MessageId} code={Code} exception={ExceptionType}", message.Id, MailSendResult.Unknown, ex.GetType().Name); return new ContactMailResult(ContactMailOutcome.Failed, MailSendResult.Unknown); } } private static string? ResolveLogo(string baseUrl, string? logoUrl) { if (string.IsNullOrWhiteSpace(logoUrl)) return null; return logoUrl.StartsWith('/') ? baseUrl + logoUrl : logoUrl; } }