2017-09-08 19:39:13 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Net.Mail;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2020-08-20 22:18:50 +01:00
|
|
|
|
using Microsoft.Extensions.Options;
|
2017-09-08 19:39:13 +02:00
|
|
|
|
using Umbraco.Core;
|
2020-08-20 22:18:50 +01:00
|
|
|
|
using Umbraco.Core.Configuration.Models;
|
2020-10-21 10:29:25 +01:00
|
|
|
|
using Umbraco.Core.HealthCheck;
|
2017-09-08 19:39:13 +02:00
|
|
|
|
using Umbraco.Core.Services;
|
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;
|
2020-10-22 13:37:47 +02:00
|
|
|
|
private readonly IEmailSender _emailSender;
|
2020-05-07 09:34:16 +02:00
|
|
|
|
|
2020-08-20 22:18:50 +01:00
|
|
|
|
private readonly ContentSettings _contentSettings;
|
2017-09-08 19:39:13 +02:00
|
|
|
|
|
2020-08-20 22:18:50 +01:00
|
|
|
|
public EmailNotificationMethod(
|
|
|
|
|
|
ILocalizedTextService textService,
|
|
|
|
|
|
IRequestAccessor requestAccessor,
|
2020-10-22 13:37:47 +02:00
|
|
|
|
IEmailSender emailSender,
|
2020-08-24 09:29:40 +02:00
|
|
|
|
IOptions<HealthChecksSettings> healthChecksSettings,
|
2020-08-23 23:36:48 +02:00
|
|
|
|
IOptions<ContentSettings> contentSettings)
|
2020-08-20 22:18:50 +01:00
|
|
|
|
: base(healthChecksSettings)
|
2017-09-08 19:39:13 +02:00
|
|
|
|
{
|
2020-03-28 14:37:31 +01: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;
|
2020-10-22 13:37:47 +02:00
|
|
|
|
_emailSender = emailSender;
|
2020-08-20 22:18:50 +01:00
|
|
|
|
_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)
|
|
|
|
|
|
});
|
|
|
|
|
|
|
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.
|
2020-05-07 09:34:16 +02:00
|
|
|
|
var host = _requestAccessor.GetApplicationUrl();
|
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
|
|
|
|
|
|
|
|
|
|
using (var mailMessage = CreateMailMessage(subject, message))
|
|
|
|
|
|
{
|
2020-10-22 13:37:47 +02:00
|
|
|
|
await _emailSender.SendAsync(mailMessage);
|
2017-09-08 19:39:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private MailMessage CreateMailMessage(string subject, string message)
|
|
|
|
|
|
{
|
2020-08-25 10:45:54 +02:00
|
|
|
|
var to = _contentSettings.Notifications.Email;
|
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("</")
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|