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

94 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 Microsoft.Extensions.Options;
2017-09-08 19:39:13 +02:00
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.HealthChecks;
using Umbraco.Core.Configuration.Models;
2017-09-08 19:39:13 +02:00
using Umbraco.Core.Services;
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 GlobalSettings _globalSettings;
private readonly ContentSettings _contentSettings;
2017-09-08 19:39:13 +02:00
public EmailNotificationMethod(
ILocalizedTextService textService,
IRequestAccessor requestAccessor,
IOptions<GlobalSettings> globalSettings,
IOptions<HealthChecksSettings> healthChecksSettings,
IOptions<ContentSettings> contentSettings)
: base(healthChecksSettings)
2017-09-08 19:39:13 +02:00
{
var recipientEmail = Settings?["recipientEmail"]?.Value;
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;
_globalSettings = globalSettings.Value;
_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, 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.
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
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 = _contentSettings.Notifications.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("</")
};
}
}
}