diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index b80dbe9..5fc1eb4 100644 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -23,6 +23,7 @@ namespace CarCareTracker.Controllers private readonly IReminderRecordDataAccess _reminderRecordDataAccess; private readonly IReminderHelper _reminderHelper; private readonly ITranslationHelper _translationHelper; + private readonly IMailHelper _mailHelper; public HomeController(ILogger logger, IVehicleDataAccess dataAccess, IUserLogic userLogic, @@ -33,7 +34,8 @@ namespace CarCareTracker.Controllers IExtraFieldDataAccess extraFieldDataAccess, IReminderRecordDataAccess reminderRecordDataAccess, IReminderHelper reminderHelper, - ITranslationHelper translationHelper) + ITranslationHelper translationHelper, + IMailHelper mailHelper) { _logger = logger; _dataAccess = dataAccess; @@ -46,6 +48,7 @@ namespace CarCareTracker.Controllers _loginLogic = loginLogic; _vehicleLogic = vehicleLogic; _translationHelper = translationHelper; + _mailHelper = mailHelper; } private int GetUserID() { @@ -572,6 +575,12 @@ namespace CarCareTracker.Controllers }; return PartialView("_ServerConfig", viewModel); } + [Authorize(Roles = nameof(UserData.IsRootUser))] + public IActionResult SendTestEmail(string emailAddress) + { + var result = _mailHelper.SendTestEmail(emailAddress); + return Json(result); + } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { diff --git a/Helper/MailHelper.cs b/Helper/MailHelper.cs index 75fb3fd..00e6fa0 100644 --- a/Helper/MailHelper.cs +++ b/Helper/MailHelper.cs @@ -11,6 +11,7 @@ namespace CarCareTracker.Helper OperationResponse NotifyUserForPasswordReset(string emailAddress, string token); OperationResponse NotifyUserForAccountUpdate(string emailAddress, string token); OperationResponse NotifyUserForReminders(Vehicle vehicle, List emailAddresses, List reminders); + OperationResponse SendTestEmail(string emailAddress); } public class MailHelper : IMailHelper { @@ -74,6 +75,28 @@ namespace CarCareTracker.Helper return OperationResponse.Failed(); } } + public OperationResponse SendTestEmail(string emailAddress) + { + if (string.IsNullOrWhiteSpace(mailConfig.EmailServer)) + { + return OperationResponse.Failed("SMTP Server Not Setup"); + } + if (string.IsNullOrWhiteSpace(emailAddress)) + { + return OperationResponse.Failed("Email Address or Token is invalid"); + } + string emailSubject = _translator.Translate(serverLanguage, "Test Email from LubeLogger"); + string emailBody = _translator.Translate(serverLanguage, "If you are seeing this email it means your SMTP configuration is functioning correctly"); + var result = SendEmail(new List { emailAddress }, emailSubject, emailBody); + if (result) + { + return OperationResponse.Succeed("Email Sent!"); + } + else + { + return OperationResponse.Failed(); + } + } public OperationResponse NotifyUserForAccountUpdate(string emailAddress, string token) { if (string.IsNullOrWhiteSpace(mailConfig.EmailServer)) diff --git a/Views/Home/_ServerConfig.cshtml b/Views/Home/_ServerConfig.cshtml index 6c4a95c..604bcae 100644 --- a/Views/Home/_ServerConfig.cshtml +++ b/Views/Home/_ServerConfig.cshtml @@ -78,7 +78,7 @@
- +
diff --git a/wwwroot/js/settings.js b/wwwroot/js/settings.js index f60ca13..04f9bc2 100644 --- a/wwwroot/js/settings.js +++ b/wwwroot/js/settings.js @@ -94,6 +94,33 @@ function updateSettings() { } }) } +function sendTestEmail() { + Swal.fire({ + title: 'Send Test Email', + html: ` + + `, + confirmButtonText: 'Send', + focusConfirm: false, + preConfirm: () => { + const emailRecipient = $("#testEmailRecipient").val(); + if (!emailRecipient || emailRecipient.trim() == '') { + Swal.showValidationMessage(`Please enter a valid email address`); + } + return { emailRecipient } + }, + }).then(function (result) { + if (result.isConfirmed) { + $.post('/Home/SendTestEmail', { emailAddress: result.value.emailRecipient }, function (data) { + if (data.success) { + successToast(data.message); + } else { + errorToast(data.message); + } + }); + } + }); +} function makeBackup() { $.get('/Files/MakeBackup', function (data) { window.location.href = data;