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

90 lines
3.3 KiB
C#
Raw Normal View History

2017-09-08 19:39:13 +02:00
using System;
using System.Net.Mail;
using System.Threading;
using System.Threading.Tasks;
using Umbraco.Core;
using Umbraco.Web.Composing;
2017-09-08 19:39:13 +02:00
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.HealthChecks;
using Umbraco.Core.Logging;
2017-09-08 19:39:13 +02:00
using Umbraco.Core.Services;
using Umbraco.Core.Configuration.UmbracoSettings;
2017-09-08 19:39:13 +02:00
namespace Umbraco.Web.HealthCheck.NotificationMethods
{
[HealthCheckNotificationMethod("email")]
public class EmailNotificationMethod : NotificationMethodBase
{
private readonly ILocalizedTextService _textService;
2018-12-06 17:24:06 +11:00
private readonly IRuntimeState _runtimeState;
private readonly ILogger _logger;
2019-12-06 12:09:47 +01:00
private readonly IGlobalSettings _globalSettings;
private readonly IUmbracoSettingsSection _umbracoSettingsSection;
2017-09-08 19:39:13 +02:00
public EmailNotificationMethod(ILocalizedTextService textService, IRuntimeState runtimeState, ILogger logger, IGlobalSettings globalSettings, IHealthChecks healthChecks, IUmbracoSettingsSection umbracoSettingsSection) : base(healthChecks)
2017-09-08 19:39:13 +02:00
{
var recipientEmail = Settings["recipientEmail"]?.Value;
if (string.IsNullOrWhiteSpace(recipientEmail))
{
Enabled = false;
return;
}
RecipientEmail = recipientEmail;
_textService = textService ?? throw new ArgumentNullException(nameof(textService));
2018-12-06 17:24:06 +11:00
_runtimeState = runtimeState;
_logger = logger;
2019-12-06 12:09:47 +01:00
_globalSettings = globalSettings;
_umbracoSettingsSection = umbracoSettingsSection ?? throw new ArgumentNullException(nameof(umbracoSettingsSection));
2017-09-08 19:39:13 +02:00
}
public string RecipientEmail { get; }
public override async Task SendAsync(HealthCheckResults results, CancellationToken token)
{
if (ShouldSend(results) == false)
{
return;
}
if (string.IsNullOrEmpty(RecipientEmail))
{
return;
}
var message = _textService.Localize("healthcheck/scheduledHealthCheckEmailBody", new[]
{
DateTime.Now.ToShortDateString(),
DateTime.Now.ToShortTimeString(),
results.ResultsAsHtml(Verbosity)
});
// Include the umbraco Application URL host in the message subject so that
// you can identify the site that these results are for.
2018-12-06 17:24:06 +11:00
var host = _runtimeState.ApplicationUrl;
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
2019-12-06 12:09:47 +01:00
var mailSender = new EmailSender(_globalSettings);
2017-09-08 19:39:13 +02:00
using (var mailMessage = CreateMailMessage(subject, message))
{
2017-09-19 15:51:47 +02:00
await mailSender.SendAsync(mailMessage);
2017-09-08 19:39:13 +02:00
}
}
private MailMessage CreateMailMessage(string subject, string message)
{
var to = _umbracoSettingsSection.Content.NotificationEmailAddress;
2017-09-08 19:39:13 +02:00
if (string.IsNullOrWhiteSpace(subject))
subject = "Umbraco Health Check Status";
return new MailMessage(to, RecipientEmail, subject, message)
{
IsBodyHtml = message.IsNullOrWhiteSpace() == false && message.Contains("<") && message.Contains("</")
};
}
}
}