diff --git a/src/Umbraco.Core/Mail/IEmailSender.cs b/src/Umbraco.Core/Mail/IEmailSender.cs index 0b4b135d03..5b5adf71eb 100644 --- a/src/Umbraco.Core/Mail/IEmailSender.cs +++ b/src/Umbraco.Core/Mail/IEmailSender.cs @@ -10,6 +10,8 @@ namespace Umbraco.Cms.Core.Mail { Task SendAsync(EmailMessage message); + Task SendAsync(EmailMessage message, bool enableNotification); + bool CanSendRequiredEmail(); } } diff --git a/src/Umbraco.Core/Mail/NotImplementedEmailSender.cs b/src/Umbraco.Core/Mail/NotImplementedEmailSender.cs index b7c7d43fb6..3632e79c23 100644 --- a/src/Umbraco.Core/Mail/NotImplementedEmailSender.cs +++ b/src/Umbraco.Core/Mail/NotImplementedEmailSender.cs @@ -9,6 +9,10 @@ namespace Umbraco.Cms.Core.Mail public Task SendAsync(EmailMessage message) => throw new NotImplementedException("To send an Email ensure IEmailSender is implemented with a custom implementation"); + public Task SendAsync(EmailMessage message, bool enableNotification) => + throw new NotImplementedException( + "To send an Email ensure IEmailSender is implemented with a custom implementation"); + public bool CanSendRequiredEmail() => throw new NotImplementedException("To send an Email ensure IEmailSender is implemented with a custom implementation"); } diff --git a/src/Umbraco.Core/Notifications/SendEmailNotification.cs b/src/Umbraco.Core/Notifications/SendEmailNotification.cs new file mode 100644 index 0000000000..194ee68edc --- /dev/null +++ b/src/Umbraco.Core/Notifications/SendEmailNotification.cs @@ -0,0 +1,11 @@ +using Umbraco.Cms.Core.Models; + +namespace Umbraco.Cms.Core.Notifications +{ + public class SendEmailNotification : INotification + { + public SendEmailNotification(EmailMessage message) => Message = message; + + public EmailMessage Message { get; set; } + } +} diff --git a/src/Umbraco.Infrastructure/EmailSender.cs b/src/Umbraco.Infrastructure/EmailSender.cs index 14b5d7e0e6..02e83405d6 100644 --- a/src/Umbraco.Infrastructure/EmailSender.cs +++ b/src/Umbraco.Infrastructure/EmailSender.cs @@ -5,8 +5,10 @@ using System.Net.Mail; using System.Threading.Tasks; using Microsoft.Extensions.Options; using Umbraco.Cms.Core.Configuration.Models; +using Umbraco.Cms.Core.Events; using Umbraco.Cms.Core.Mail; using Umbraco.Cms.Core.Models; +using Umbraco.Cms.Core.Notifications; using Umbraco.Cms.Infrastructure.Extensions; using SmtpClient = MailKit.Net.Smtp.SmtpClient; @@ -18,20 +20,41 @@ namespace Umbraco.Cms.Infrastructure public class EmailSender : IEmailSender { // TODO: This should encapsulate a BackgroundTaskRunner with a queue to send these emails! - + private readonly IEventAggregator _eventAggregator; private readonly GlobalSettings _globalSettings; + private readonly bool _notificationHandlerRegistered; - public EmailSender(IOptions globalSettings) => _globalSettings = globalSettings.Value; + public EmailSender(IOptions globalSettings, IEventAggregator eventAggregator) + : this(globalSettings, eventAggregator, null) + { + + } + + public EmailSender(IOptions globalSettings, IEventAggregator eventAggregator, INotificationHandler handler) + { + _eventAggregator = eventAggregator; + _globalSettings = globalSettings.Value; + _notificationHandlerRegistered = handler is not null; + } /// /// Sends the message async /// /// /// - public async Task SendAsync(EmailMessage message) + public async Task SendAsync(EmailMessage message) => await SendAsyncInternal(message, false); + + public async Task SendAsync(EmailMessage message, bool enableNotification) => + await SendAsyncInternal(message, enableNotification); + + private async Task SendAsyncInternal(EmailMessage message, bool enableNotification) { if (_globalSettings.IsSmtpServerConfigured == false) { + if (enableNotification) + { + await _eventAggregator.PublishAsync(new SendEmailNotification(message)); + } return; } @@ -66,6 +89,6 @@ namespace Umbraco.Cms.Infrastructure /// /// We assume this is possible if either an event handler is registered or an smtp server is configured /// - public bool CanSendRequiredEmail() => _globalSettings.IsSmtpServerConfigured; + public bool CanSendRequiredEmail() => _globalSettings.IsSmtpServerConfigured || _notificationHandlerRegistered; } } diff --git a/src/Umbraco.Tests.UnitTests/TestHelpers/TestHelper.cs b/src/Umbraco.Tests.UnitTests/TestHelpers/TestHelper.cs index 52eb25af48..f432787dc4 100644 --- a/src/Umbraco.Tests.UnitTests/TestHelpers/TestHelper.cs +++ b/src/Umbraco.Tests.UnitTests/TestHelpers/TestHelper.cs @@ -20,6 +20,7 @@ using Umbraco.Cms.Core.Composing; using Umbraco.Cms.Core.Configuration; using Umbraco.Cms.Core.Configuration.Models; using Umbraco.Cms.Core.Diagnostics; +using Umbraco.Cms.Core.Events; using Umbraco.Cms.Core.Hosting; using Umbraco.Cms.Core.IO; using Umbraco.Cms.Core.Logging; @@ -136,7 +137,7 @@ namespace Umbraco.Cms.Tests.UnitTests.TestHelpers public static UriUtility UriUtility => s_testHelperInternal.UriUtility; - public static IEmailSender EmailSender { get; } = new EmailSender(Options.Create(new GlobalSettings())); + public static IEmailSender EmailSender { get; } = new EmailSender(Options.Create(new GlobalSettings()), Mock.Of()); /// /// Some test files are copied to the /bin (/bin/debug) on build, this is a utility to return their physical path based on a virtual path name diff --git a/src/Umbraco.Web.BackOffice/Controllers/UsersController.cs b/src/Umbraco.Web.BackOffice/Controllers/UsersController.cs index 2d7418b6bd..cdd00913e7 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/UsersController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/UsersController.cs @@ -552,7 +552,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers var mailMessage = new EmailMessage(fromEmail, to.Email, emailSubject, emailBody, true); - await _emailSender.SendAsync(mailMessage); + await _emailSender.SendAsync(mailMessage, true); } ///