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:
@@ -0,0 +1,97 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SlpModularCms.Core.Email;
|
||||
using SlpModularCms.Core.SiteSettings;
|
||||
using SlpModularCms.Modules.Contact.Config;
|
||||
using SlpModularCms.Modules.Contact.Data.Entities;
|
||||
|
||||
namespace SlpModularCms.Modules.Contact.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public sealed class ContactMailSender(
|
||||
IEmailSender sender,
|
||||
IMailTemplateRenderer renderer,
|
||||
ILanguageResolver languages,
|
||||
ISiteSettingsService siteSettings,
|
||||
IMailAvailability availability,
|
||||
IOptions<EmailOptions> email,
|
||||
IOptions<AppMailOptions> app,
|
||||
IOptions<ContactOptions> contact,
|
||||
ILogger<ContactMailSender> logger) : IContactMailSender
|
||||
{
|
||||
public string? NotReadyReason()
|
||||
{
|
||||
if (!availability.IsEnabled)
|
||||
{
|
||||
return ContactMailResult.Disabled;
|
||||
}
|
||||
|
||||
return EmailAddressRules.IsValidAddress(contact.Value.RecipientEmail) ? null : ContactMailResult.RecipientNotConfigured;
|
||||
}
|
||||
|
||||
public async Task<ContactMailResult> SendAsync(ContactMessage message, CancellationToken cancellationToken)
|
||||
{
|
||||
var notReady = NotReadyReason();
|
||||
if (notReady is not null)
|
||||
{
|
||||
return new ContactMailResult(ContactMailOutcome.Skipped, notReady);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var (fromAddress, fromName) = await siteSettings.GetEffectiveSenderAsync(cancellationToken);
|
||||
var baseUrl = app.Value.PublicBaseUrl.TrimEnd('/');
|
||||
var language = languages.Resolve(null);
|
||||
var dutch = language == MailLanguages.Dutch;
|
||||
|
||||
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(dutch ? "Naam" : "Name", message.Name),
|
||||
new MailDetail(dutch ? "E-mailadres" : "E-mail address", message.Email),
|
||||
new MailDetail(dutch ? "Onderwerp" : "Subject", message.Subject),
|
||||
new MailDetail(dutch ? "Bericht" : "Message", message.Message),
|
||||
]);
|
||||
|
||||
var rendered = renderer.Render(MailKind.ContactMessage, language, model);
|
||||
var result = await sender.SendAsync(
|
||||
new OutgoingMail(
|
||||
contact.Value.RecipientEmail!.Trim(), 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user