2017-09-08 19:39:13 +02:00
|
|
|
|
using System;
|
2018-11-27 21:16:24 +00:00
|
|
|
|
using System.Collections.Generic;
|
2017-09-08 19:39:13 +02:00
|
|
|
|
using System.Net.Mail;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Umbraco.Core;
|
2018-12-12 17:49:24 +01:00
|
|
|
|
using Umbraco.Core.Composing;
|
2017-09-08 19:39:13 +02:00
|
|
|
|
using Umbraco.Core.Configuration;
|
2018-11-27 21:16:24 +00:00
|
|
|
|
using Umbraco.Core.Logging;
|
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;
|
2018-12-06 17:24:06 +11:00
|
|
|
|
private readonly IRuntimeState _runtimeState;
|
|
|
|
|
|
private readonly ILogger _logger;
|
2017-09-08 19:39:13 +02:00
|
|
|
|
|
2018-12-06 17:24:06 +11:00
|
|
|
|
public EmailNotificationMethod(ILocalizedTextService textService, IRuntimeState runtimeState, ILogger logger)
|
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;
|
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)
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2018-11-27 21:16:24 +00:00
|
|
|
|
// 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-11-27 21:16:24 +00:00
|
|
|
|
|
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
|
|
|
|
|
2017-09-19 15:51:47 +02:00
|
|
|
|
var mailSender = new EmailSender();
|
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)
|
|
|
|
|
|
{
|
2018-12-12 17:49:24 +01:00
|
|
|
|
var to = Current.Config.Umbraco().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("</")
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|