Files
Umbraco-CMS/src/Umbraco.Core/HealthCheck/NotificationMethods/EmailNotificationMethod.cs

94 lines
3.4 KiB
C#
Raw Normal View History

2017-09-08 19:39:13 +02:00
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
2017-09-08 19:39:13 +02:00
using Umbraco.Core;
using Umbraco.Core.Configuration.Models;
Netcore/feature/healthcheck replaceconfiglogic (#8934) * Started moving to JSON instead of config. Mild refactoring but overall keeping it the same, except for injecting IConfiguration and JSON parsing instead of XML Tests currently unaffected, need to increase coverage * Moved to constants for appsettings keys Moved from IConfiguration to global settings - later to be replaced with IOptions Updated translation messages Installed IOptions ready for new PR Updated to new interface, IConfigurationService * Post-merge fix * Namespace move from Umbraco.Web to Umbraco.Core where appropriate * Renamed abstractsettings (from abstractconfig) Moving out of configservice into the POCO config check * Made the IIsCustomErrors healthcheck as obsolete, as no web.config setting). Investigate reintroducing this check in the .NET Core way (UseDevelopment). Reducing use of abstractsettings as not needed - we don't need a config service to read the config settings anymore as they're all explicit POCOs. Consolidated health-checks in project. * Removed test views that weren't meant to be added * Returned to use of abstractsettings with different use * Moved more health checks into correct folder/namespace, and enum into their own file * Correct namespace * Git history/compare lost due to file move, temporarily moving back to original folder. Will do another PR to move after this * Use existing GetStatus in abstract check for Debug mode * Updating to return to previous logic and putting files back into line * Macro errors returned to previous logic * Reuse abstractsettings class * Swapped order to assist with reviewing PR * Updated to include itempath * Not implemented comment to avoid confusion Implemented NotificationEmailCheck * Changed to IOptionsMonitor as per PR comments. Removed configurationservice as we need to rethink the fixing strategy. Updated logger. Needs to show fix message instead of fixing. Temporary fix for IIS version * Switched to IOptionsMonitor for all * No longer attempts to actually fix header in config. Still need to show suggestions. Co-authored-by: Elitsa Marinovska <elm@umbraco.dk>
2020-10-21 10:29:25 +01:00
using Umbraco.Core.HealthCheck;
using Umbraco.Core.Mail;
2020-10-22 15:08:07 +02:00
using Umbraco.Core.Models;
2017-09-08 19:39:13 +02:00
using Umbraco.Core.Services;
Netcore/feature/healthcheck replaceconfiglogic (#8934) * Started moving to JSON instead of config. Mild refactoring but overall keeping it the same, except for injecting IConfiguration and JSON parsing instead of XML Tests currently unaffected, need to increase coverage * Moved to constants for appsettings keys Moved from IConfiguration to global settings - later to be replaced with IOptions Updated translation messages Installed IOptions ready for new PR Updated to new interface, IConfigurationService * Post-merge fix * Namespace move from Umbraco.Web to Umbraco.Core where appropriate * Renamed abstractsettings (from abstractconfig) Moving out of configservice into the POCO config check * Made the IIsCustomErrors healthcheck as obsolete, as no web.config setting). Investigate reintroducing this check in the .NET Core way (UseDevelopment). Reducing use of abstractsettings as not needed - we don't need a config service to read the config settings anymore as they're all explicit POCOs. Consolidated health-checks in project. * Removed test views that weren't meant to be added * Returned to use of abstractsettings with different use * Moved more health checks into correct folder/namespace, and enum into their own file * Correct namespace * Git history/compare lost due to file move, temporarily moving back to original folder. Will do another PR to move after this * Use existing GetStatus in abstract check for Debug mode * Updating to return to previous logic and putting files back into line * Macro errors returned to previous logic * Reuse abstractsettings class * Swapped order to assist with reviewing PR * Updated to include itempath * Not implemented comment to avoid confusion Implemented NotificationEmailCheck * Changed to IOptionsMonitor as per PR comments. Removed configurationservice as we need to rethink the fixing strategy. Updated logger. Needs to show fix message instead of fixing. Temporary fix for IIS version * Switched to IOptionsMonitor for all * No longer attempts to actually fix header in config. Still need to show suggestions. Co-authored-by: Elitsa Marinovska <elm@umbraco.dk>
2020-10-21 10:29:25 +01:00
using Umbraco.Infrastructure.HealthCheck;
2017-09-08 19:39:13 +02:00
namespace Umbraco.Web.HealthCheck.NotificationMethods
{
[HealthCheckNotificationMethod("email")]
public class EmailNotificationMethod : NotificationMethodBase
{
private readonly ILocalizedTextService _textService;
2020-05-07 09:34:16 +02:00
private readonly IRequestAccessor _requestAccessor;
private readonly IEmailSender _emailSender;
private readonly IMarkdownToHtmlConverter _markdownToHtmlConverter;
2020-05-07 09:34:16 +02:00
private readonly ContentSettings _contentSettings;
2017-09-08 19:39:13 +02:00
public EmailNotificationMethod(
ILocalizedTextService textService,
IRequestAccessor requestAccessor,
IEmailSender emailSender,
IOptions<HealthChecksSettings> healthChecksSettings,
IOptions<ContentSettings> contentSettings,
IMarkdownToHtmlConverter markdownToHtmlConverter)
: base(healthChecksSettings)
2017-09-08 19:39:13 +02:00
{
var recipientEmail = Settings?["RecipientEmail"];
2017-09-08 19:39:13 +02:00
if (string.IsNullOrWhiteSpace(recipientEmail))
{
Enabled = false;
return;
}
RecipientEmail = recipientEmail;
_textService = textService ?? throw new ArgumentNullException(nameof(textService));
2020-05-07 09:34:16 +02:00
_requestAccessor = requestAccessor;
_emailSender = emailSender;
_markdownToHtmlConverter = markdownToHtmlConverter;
_contentSettings = contentSettings.Value ?? throw new ArgumentNullException(nameof(contentSettings));
2017-09-08 19:39:13 +02:00
}
public string RecipientEmail { get; }
public override async Task SendAsync(HealthCheckResults results)
2017-09-08 19:39:13 +02:00
{
if (ShouldSend(results) == false)
{
return;
}
if (string.IsNullOrEmpty(RecipientEmail))
{
return;
}
var message = _textService.Localize("healthcheck/scheduledHealthCheckEmailBody", new[]
{
DateTime.Now.ToShortDateString(),
DateTime.Now.ToShortTimeString(),
_markdownToHtmlConverter.ToHtml(results, Verbosity)
2017-09-08 19:39:13 +02:00
});
// Include the umbraco Application URL host in the message subject so that
// you can identify the site that these results are for.
2020-05-07 09:34:16 +02:00
var host = _requestAccessor.GetApplicationUrl();
2018-12-06 17:24:06 +11:00
var subject = _textService.Localize("healthcheck/scheduledHealthCheckEmailSubject", new[] { host.ToString() });
2017-09-08 19:39:13 +02:00
2020-10-22 15:08:07 +02:00
var mailMessage = CreateMailMessage(subject, message);
await _emailSender.SendAsync(mailMessage);
2017-09-08 19:39:13 +02:00
}
2020-10-22 15:08:07 +02:00
private EmailMessage CreateMailMessage(string subject, string message)
2017-09-08 19:39:13 +02:00
{
var to = _contentSettings.Notifications.Email;
2017-09-08 19:39:13 +02:00
if (string.IsNullOrWhiteSpace(subject))
subject = "Umbraco Health Check Status";
var isBodyHtml = message.IsNullOrWhiteSpace() == false && message.Contains("<") && message.Contains("</");
return new EmailMessage(to, RecipientEmail, subject, message, isBodyHtml);
2017-09-08 19:39:13 +02:00
}
}
}