diff --git a/src/Umbraco.Core/Constants-Web.cs b/src/Umbraco.Core/Constants-Web.cs index 13cd7d7ad3..f6a8c00970 100644 --- a/src/Umbraco.Core/Constants-Web.cs +++ b/src/Umbraco.Core/Constants-Web.cs @@ -59,6 +59,15 @@ namespace Umbraco.Cms.Core public const string ActionToken = "action"; public const string AreaToken = "area"; } + + public static class EmailTypes + { + public const string HealthCheck = "HealthCheck"; + public const string Notification = "Notification"; + public const string PasswordReset = "PasswordReset"; + public const string TwoFactorAuth = "2FA"; + public const string UserInvite = "UserInvite"; + } } } } diff --git a/src/Umbraco.Core/HealthChecks/NotificationMethods/EmailNotificationMethod.cs b/src/Umbraco.Core/HealthChecks/NotificationMethods/EmailNotificationMethod.cs index 0811feeb7d..8de4cd8cc3 100644 --- a/src/Umbraco.Core/HealthChecks/NotificationMethods/EmailNotificationMethod.cs +++ b/src/Umbraco.Core/HealthChecks/NotificationMethods/EmailNotificationMethod.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Threading.Tasks; using Microsoft.Extensions.Options; using Umbraco.Cms.Core.Configuration.Models; @@ -74,7 +74,7 @@ namespace Umbraco.Cms.Core.HealthChecks.NotificationMethods var mailMessage = CreateMailMessage(subject, message); - await _emailSender.SendAsync(mailMessage); + await _emailSender.SendAsync(mailMessage, Constants.Web.EmailTypes.HealthCheck); } private EmailMessage CreateMailMessage(string subject, string message) diff --git a/src/Umbraco.Core/Mail/IEmailSender.cs b/src/Umbraco.Core/Mail/IEmailSender.cs index 45959a5a9a..0c573c542c 100644 --- a/src/Umbraco.Core/Mail/IEmailSender.cs +++ b/src/Umbraco.Core/Mail/IEmailSender.cs @@ -8,9 +8,9 @@ namespace Umbraco.Cms.Core.Mail /// public interface IEmailSender { - Task SendAsync(EmailMessage message); + Task SendAsync(EmailMessage message, string emailType); - Task SendAsync(EmailMessage message, bool enableNotification); + Task SendAsync(EmailMessage message, string emailType, bool enableNotification); bool CanSendRequiredEmail(); } diff --git a/src/Umbraco.Core/Mail/NotImplementedEmailSender.cs b/src/Umbraco.Core/Mail/NotImplementedEmailSender.cs index 29265b4038..15e36767d9 100644 --- a/src/Umbraco.Core/Mail/NotImplementedEmailSender.cs +++ b/src/Umbraco.Core/Mail/NotImplementedEmailSender.cs @@ -6,10 +6,10 @@ namespace Umbraco.Cms.Core.Mail { internal class NotImplementedEmailSender : IEmailSender { - public Task SendAsync(EmailMessage message) + public Task SendAsync(EmailMessage message, string emailType) => throw new NotImplementedException("To send an Email ensure IEmailSender is implemented with a custom implementation"); - public Task SendAsync(EmailMessage message, bool enableNotification) => + public Task SendAsync(EmailMessage message, string emailType, bool enableNotification) => 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 index 4ca6dc80c0..f87a2a0ba8 100644 --- a/src/Umbraco.Core/Notifications/SendEmailNotification.cs +++ b/src/Umbraco.Core/Notifications/SendEmailNotification.cs @@ -4,10 +4,19 @@ namespace Umbraco.Cms.Core.Notifications { public class SendEmailNotification : INotification { - public SendEmailNotification(NotificationEmailModel message) => Message = message; + public SendEmailNotification(NotificationEmailModel message, string emailType) + { + Message = message; + EmailType = emailType; + } public NotificationEmailModel Message { get; } + /// + /// Some metadata about the email which can be used by handlers to determine if they should handle the email or not + /// + public string EmailType { get; } + /// /// Call to tell Umbraco that the email sending is handled. /// diff --git a/src/Umbraco.Infrastructure/Mail/EmailSender.cs b/src/Umbraco.Infrastructure/Mail/EmailSender.cs index 4c48a759fe..95be66e063 100644 --- a/src/Umbraco.Infrastructure/Mail/EmailSender.cs +++ b/src/Umbraco.Infrastructure/Mail/EmailSender.cs @@ -49,16 +49,16 @@ namespace Umbraco.Cms.Infrastructure.Mail /// /// /// - public async Task SendAsync(EmailMessage message) => await SendAsyncInternal(message, false); + public async Task SendAsync(EmailMessage message, string emailType) => await SendAsyncInternal(message, emailType, false); - public async Task SendAsync(EmailMessage message, bool enableNotification) => - await SendAsyncInternal(message, enableNotification); + public async Task SendAsync(EmailMessage message, string emailType, bool enableNotification) => + await SendAsyncInternal(message, emailType, enableNotification); - private async Task SendAsyncInternal(EmailMessage message, bool enableNotification) + private async Task SendAsyncInternal(EmailMessage message, string emailType, bool enableNotification) { if (enableNotification) { - var notification = new SendEmailNotification(message.ToNotificationEmail(_globalSettings.Smtp?.From)); + var notification = new SendEmailNotification(message.ToNotificationEmail(_globalSettings.Smtp?.From), emailType); await _eventAggregator.PublishAsync(notification); // if a handler handled sending the email then don't continue. diff --git a/src/Umbraco.Infrastructure/Services/Implement/NotificationService.cs b/src/Umbraco.Infrastructure/Services/Implement/NotificationService.cs index c6f440942e..dc45319d12 100644 --- a/src/Umbraco.Infrastructure/Services/Implement/NotificationService.cs +++ b/src/Umbraco.Infrastructure/Services/Implement/NotificationService.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Globalization; @@ -519,7 +519,7 @@ namespace Umbraco.Cms.Core.Services.Implement { try { - _emailSender.SendAsync(request.Mail).GetAwaiter().GetResult(); + _emailSender.SendAsync(request.Mail, Constants.Web.EmailTypes.Notification).GetAwaiter().GetResult(); _logger.LogDebug("Notification '{Action}' sent to {Username} ({Email})", request.Action, request.UserName, request.Email); } catch (Exception ex) diff --git a/src/Umbraco.Web.BackOffice/Controllers/AuthenticationController.cs b/src/Umbraco.Web.BackOffice/Controllers/AuthenticationController.cs index cf7a7dd729..54bfbeeb5f 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/AuthenticationController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/AuthenticationController.cs @@ -394,7 +394,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers var mailMessage = new EmailMessage(from, user.Email, subject, message, true); - await _emailSender.SendAsync(mailMessage); + await _emailSender.SendAsync(mailMessage, Constants.Web.EmailTypes.PasswordReset); _userManager.NotifyForgotPasswordRequested(User, user.Id.ToString()); } @@ -458,7 +458,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers { var mailMessage = new EmailMessage(from, user.Email, subject, message, true); - await _emailSender.SendAsync(mailMessage); + await _emailSender.SendAsync(mailMessage, Constants.Web.EmailTypes.TwoFactorAuth); } else if (provider == "Phone") { diff --git a/src/Umbraco.Web.BackOffice/Controllers/UsersController.cs b/src/Umbraco.Web.BackOffice/Controllers/UsersController.cs index 00e486fad5..821719c796 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/UsersController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/UsersController.cs @@ -559,7 +559,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers var mailMessage = new EmailMessage(fromEmail, toMailBoxAddress.ToString(), emailSubject, emailBody, true); - await _emailSender.SendAsync(mailMessage, true); + await _emailSender.SendAsync(mailMessage, Constants.Web.EmailTypes.UserInvite, true); } ///