From ab84490535fcc315d1daf36b202f93ebb7cc2741 Mon Sep 17 00:00:00 2001 From: Shannon Date: Mon, 9 Aug 2021 13:42:08 -0600 Subject: [PATCH] Fixes email sending notifications so they are checked if they are handled. --- .../Notifications/SendEmailNotification.cs | 10 +++ src/Umbraco.Infrastructure/EmailSender.cs | 71 +++++++++++-------- 2 files changed, 51 insertions(+), 30 deletions(-) diff --git a/src/Umbraco.Core/Notifications/SendEmailNotification.cs b/src/Umbraco.Core/Notifications/SendEmailNotification.cs index 3c9caabb0e..4ca6dc80c0 100644 --- a/src/Umbraco.Core/Notifications/SendEmailNotification.cs +++ b/src/Umbraco.Core/Notifications/SendEmailNotification.cs @@ -7,5 +7,15 @@ namespace Umbraco.Cms.Core.Notifications public SendEmailNotification(NotificationEmailModel message) => Message = message; public NotificationEmailModel Message { get; } + + /// + /// Call to tell Umbraco that the email sending is handled. + /// + public void HandleEmail() => IsHandled = true; + + /// + /// Returns true if the email sending is handled. + /// + public bool IsHandled { get; private set; } } } diff --git a/src/Umbraco.Infrastructure/EmailSender.cs b/src/Umbraco.Infrastructure/EmailSender.cs index 72daad7de1..20c48a3b04 100644 --- a/src/Umbraco.Infrastructure/EmailSender.cs +++ b/src/Umbraco.Infrastructure/EmailSender.cs @@ -3,6 +3,7 @@ using System.Net.Mail; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Umbraco.Cms.Core.Configuration.Models; using Umbraco.Cms.Core.Events; @@ -23,12 +24,14 @@ namespace Umbraco.Cms.Infrastructure private readonly IEventAggregator _eventAggregator; private readonly GlobalSettings _globalSettings; private readonly bool _notificationHandlerRegistered; + private readonly ILogger _logger; - public EmailSender(IOptions globalSettings, IEventAggregator eventAggregator) + public EmailSender( + ILogger logger, + IOptions globalSettings, + IEventAggregator eventAggregator) : this(globalSettings, eventAggregator, null) - { - - } + => _logger = logger; public EmailSender(IOptions globalSettings, IEventAggregator eventAggregator, INotificationHandler handler) { @@ -49,39 +52,47 @@ namespace Umbraco.Cms.Infrastructure private async Task SendAsyncInternal(EmailMessage message, bool enableNotification) { + if (enableNotification) + { + var notification = new SendEmailNotification(message.ToNotificationEmail(_globalSettings.Smtp?.From)); + await _eventAggregator.PublishAsync(notification); + + // if a handler handled sending the email then don't continue. + if (notification.IsHandled) + { + _logger.LogDebug("The email sending for {Subject} was handled by a notification handler", notification.Message.Subject); + return; + } + } + if (_globalSettings.IsSmtpServerConfigured == false) { - if (enableNotification) - { - await _eventAggregator.PublishAsync( - new SendEmailNotification(message.ToNotificationEmail(_globalSettings.Smtp?.From))); - } + _logger.LogDebug("Could not send email for {Subject}. It was not handled by a notification handler and there is no SMTP configured.", message.Subject); return; } - using (var client = new SmtpClient()) + using var client = new SmtpClient(); + + await client.ConnectAsync(_globalSettings.Smtp.Host, + _globalSettings.Smtp.Port, + (MailKit.Security.SecureSocketOptions)(int)_globalSettings.Smtp.SecureSocketOptions); + + if (!(_globalSettings.Smtp.Username is null && _globalSettings.Smtp.Password is null)) { - await client.ConnectAsync(_globalSettings.Smtp.Host, - _globalSettings.Smtp.Port, - (MailKit.Security.SecureSocketOptions)(int)_globalSettings.Smtp.SecureSocketOptions); - - if (!(_globalSettings.Smtp.Username is null && _globalSettings.Smtp.Password is null)) - { - await client.AuthenticateAsync(_globalSettings.Smtp.Username, _globalSettings.Smtp.Password); - } - - var mailMessage = message.ToMimeMessage(_globalSettings.Smtp.From); - if (_globalSettings.Smtp.DeliveryMethod == SmtpDeliveryMethod.Network) - { - await client.SendAsync(mailMessage); - } - else - { - client.Send(mailMessage); - } - - await client.DisconnectAsync(true); + await client.AuthenticateAsync(_globalSettings.Smtp.Username, _globalSettings.Smtp.Password); } + + var mailMessage = message.ToMimeMessage(_globalSettings.Smtp.From); + if (_globalSettings.Smtp.DeliveryMethod == SmtpDeliveryMethod.Network) + { + await client.SendAsync(mailMessage); + } + else + { + client.Send(mailMessage); + } + + await client.DisconnectAsync(true); } ///