From e11d0871c43dc97317d4051cf4b906f24407fa0c Mon Sep 17 00:00:00 2001 From: Gary Sharp Date: Fri, 5 Feb 2021 15:24:20 +1100 Subject: [PATCH] feature: add reply-to address for email service --- Disco.Models/Services/Messaging/Email.cs | 1 + Disco.Services/Messaging/EmailService.cs | 12 ++- .../Areas/API/Controllers/SystemController.cs | 5 +- .../Config/Models/SystemConfig/IndexModel.cs | 2 + .../Config/Views/SystemConfig/Index.cshtml | 17 +++ .../Views/SystemConfig/Index.generated.cs | 101 +++++++++++++----- .../T4MVC/API.SystemController.generated.cs | 8 +- 7 files changed, 112 insertions(+), 34 deletions(-) diff --git a/Disco.Models/Services/Messaging/Email.cs b/Disco.Models/Services/Messaging/Email.cs index 68ef4ecf..a7ba8ad0 100644 --- a/Disco.Models/Services/Messaging/Email.cs +++ b/Disco.Models/Services/Messaging/Email.cs @@ -5,6 +5,7 @@ namespace Disco.Models.Services.Messaging public class Email { public string From { get; set; } + public string ReplyTo { get; set; } public List To { get; } = new List(); public List CC { get; } = new List(); public List BCC { get; } = new List(); diff --git a/Disco.Services/Messaging/EmailService.cs b/Disco.Services/Messaging/EmailService.cs index 0d535f23..86e6ff45 100644 --- a/Disco.Services/Messaging/EmailService.cs +++ b/Disco.Services/Messaging/EmailService.cs @@ -15,6 +15,7 @@ namespace Disco.Services.Messaging private static bool smtpEnableSsl; private static string smtpUsername; private static string smtpPassword; + private static string smtpReplyToAddress; public static bool IsConfigured { get; private set; } @@ -26,7 +27,7 @@ namespace Disco.Services.Messaging } } - public static void ValidateConfiguration(string smtpServer, int smtpPort, string fromAddress, bool enableSsl, string username, string password) + public static void ValidateConfiguration(string smtpServer, int smtpPort, string fromAddress, string replyToAddress, bool enableSsl, string username, string password) { // if smtpServer is null, we aren't configured (emailing is disabled) if (!string.IsNullOrWhiteSpace(smtpServer)) @@ -38,6 +39,9 @@ namespace Disco.Services.Messaging throw new ArgumentOutOfRangeException(nameof(fromAddress), "From Address is required"); // try parse FromAddress new MailAddress(fromAddress); + // try parse reply-to address + if (!string.IsNullOrWhiteSpace(replyToAddress)) + new MailAddress(replyToAddress); } } @@ -49,6 +53,7 @@ namespace Disco.Services.Messaging smtpFromAddress = systemConfiguration.EmailFromAddress; smtpUsername = systemConfiguration.EmailUsername; smtpPassword = systemConfiguration.EmailPassword; + smtpReplyToAddress = systemConfiguration.EmailReplyToAddress; IsConfigured = !string.IsNullOrWhiteSpace(smtpServer) && @@ -73,6 +78,11 @@ namespace Disco.Services.Messaging else message.From = new MailAddress(smtpFromAddress); + if (!string.IsNullOrWhiteSpace(email.ReplyTo)) + message.ReplyToList.Add(new MailAddress(email.ReplyTo)); + else if (!string.IsNullOrWhiteSpace(smtpReplyToAddress)) + message.ReplyToList.Add(new MailAddress(smtpReplyToAddress)); + if (email.To.Count > 0) { foreach (var recipient in email.To) diff --git a/Disco.Web/Areas/API/Controllers/SystemController.cs b/Disco.Web/Areas/API/Controllers/SystemController.cs index 5965873d..9f698085 100644 --- a/Disco.Web/Areas/API/Controllers/SystemController.cs +++ b/Disco.Web/Areas/API/Controllers/SystemController.cs @@ -368,18 +368,19 @@ namespace Disco.Web.Areas.API.Controllers #region Email Settings [DiscoAuthorize(Claims.Config.System.ConfigureEmail), ValidateInput(false), ValidateAntiForgeryToken] - public virtual ActionResult UpdateEmailSettings(string SmtpServer, int? SmtpPort, string FromAddress, bool EnableSsl, string Username, string Password, bool redirect = false) + public virtual ActionResult UpdateEmailSettings(string SmtpServer, int? SmtpPort, string FromAddress, string ReplyToAddress, bool EnableSsl, string Username, string Password, bool redirect = false) { // Default Port if (!SmtpPort.HasValue) SmtpPort = 25; - EmailService.ValidateConfiguration(SmtpServer, SmtpPort.Value, FromAddress, EnableSsl, Username, Password); + EmailService.ValidateConfiguration(SmtpServer, SmtpPort.Value, FromAddress, ReplyToAddress, EnableSsl, Username, Password); SystemConfiguration config = Database.DiscoConfiguration; config.EmailSmtpServer = SmtpServer; config.EmailSmtpPort = SmtpPort.Value; config.EmailFromAddress = FromAddress; + config.EmailReplyToAddress = ReplyToAddress; config.EmailEnableSsl = EnableSsl; config.EmailUsername = Username; config.EmailPassword = Password; diff --git a/Disco.Web/Areas/Config/Models/SystemConfig/IndexModel.cs b/Disco.Web/Areas/Config/Models/SystemConfig/IndexModel.cs index 3e083735..6e5e3154 100644 --- a/Disco.Web/Areas/Config/Models/SystemConfig/IndexModel.cs +++ b/Disco.Web/Areas/Config/Models/SystemConfig/IndexModel.cs @@ -103,6 +103,7 @@ namespace Disco.Web.Areas.Config.Models.SystemConfig public string EmailSmtpServer { get; set; } public int EmailSmtpPort { get; set; } public string EmailFromAddress { get; set; } + public string EmailReplyToAddress { get; set; } [Display(Name = "Enable SSL")] public bool EmailEnableSsl { get; set; } public string EmailUsername { get; set; } @@ -130,6 +131,7 @@ namespace Disco.Web.Areas.Config.Models.SystemConfig EmailSmtpServer = config.EmailSmtpServer, EmailSmtpPort = config.EmailSmtpPort, EmailFromAddress = config.EmailFromAddress, + EmailReplyToAddress = config.EmailReplyToAddress, EmailEnableSsl = config.EmailEnableSsl, EmailUsername = config.EmailUsername, EmailPassword = config.EmailPassword, diff --git a/Disco.Web/Areas/Config/Views/SystemConfig/Index.cshtml b/Disco.Web/Areas/Config/Views/SystemConfig/Index.cshtml index d4cf1bd1..b21f32cb 100644 --- a/Disco.Web/Areas/Config/Views/SystemConfig/Index.cshtml +++ b/Disco.Web/Areas/Config/Views/SystemConfig/Index.cshtml @@ -696,6 +696,15 @@ else @Html.ValidationMessageFor(m => m.EmailFromAddress) + + + Reply To Address: + + + @Html.EditorFor(m => m.EmailReplyToAddress)
+ @Html.ValidationMessageFor(m => m.EmailReplyToAddress) + +   @@ -848,6 +857,14 @@ else @Html.DisplayFor(m => m.EmailFromAddress) + + + Reply To Address: + + + @Html.DisplayFor(m => m.EmailReplyToAddress) + + Enable SSL: diff --git a/Disco.Web/Areas/Config/Views/SystemConfig/Index.generated.cs b/Disco.Web/Areas/Config/Views/SystemConfig/Index.generated.cs index 44360195..d0c2a5a3 100644 --- a/Disco.Web/Areas/Config/Views/SystemConfig/Index.generated.cs +++ b/Disco.Web/Areas/Config/Views/SystemConfig/Index.generated.cs @@ -1982,12 +1982,40 @@ WriteLiteral("\r\n \r\n \r\n WriteLiteral(" style=\"width: 135px\""); -WriteLiteral(">\r\n  \r\n \r\n \r\n"); +WriteLiteral(">\r\n Reply To Address:\r\n \r\n " + +"\r\n"); WriteLiteral(" "); #line 704 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + Write(Html.EditorFor(m => m.EmailReplyToAddress)); + + + #line default + #line hidden +WriteLiteral("
\r\n"); + +WriteLiteral(" "); + + + #line 705 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + Write(Html.ValidationMessageFor(m => m.EmailReplyToAddress)); + + + #line default + #line hidden +WriteLiteral("\r\n \r\n \r\n \r\n \r\n  \r\n \r\n \r\n"); + +WriteLiteral(" "); + + + #line 713 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" Write(Html.CheckBoxFor(m => m.EmailEnableSsl)); @@ -1996,7 +2024,7 @@ WriteLiteral(" "); WriteLiteral(" "); - #line 704 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + #line 713 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" Write(Html.LabelFor(m => m.EmailEnableSsl)); @@ -2012,7 +2040,7 @@ WriteLiteral(">\r\n Username:\r\n \r\n WriteLiteral(" "); - #line 712 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + #line 721 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" Write(Html.EditorFor(m => m.EmailUsername)); @@ -2023,7 +2051,7 @@ WriteLiteral("
\r\n"); WriteLiteral(" "); - #line 713 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + #line 722 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" Write(Html.ValidationMessageFor(m => m.EmailUsername)); @@ -2039,7 +2067,7 @@ WriteLiteral(">\r\n Password:\r\n \r\n WriteLiteral(" "); - #line 721 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + #line 730 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" Write(Html.EditorFor(m => m.EmailPassword)); @@ -2050,7 +2078,7 @@ WriteLiteral("
\r\n"); WriteLiteral(" "); - #line 722 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + #line 731 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" Write(Html.ValidationMessageFor(m => m.EmailPassword)); @@ -2066,7 +2094,7 @@ WriteLiteral(">\r\n  \r\n \r\n WriteLiteral(" "); - #line 730 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + #line 739 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" Write(Html.AntiForgeryToken()); @@ -2083,7 +2111,7 @@ WriteLiteral(" class=\"button small\""); WriteLiteral(" "); - #line 731 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + #line 740 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" Write(Model.EmailIsConfigured ? null : "disabled"); @@ -2100,7 +2128,7 @@ WriteLiteral(" class=\"button small\""); WriteLiteral(">Save Email Settings"); - #line 732 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + #line 741 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" Write(AjaxHelpers.AjaxLoader()); @@ -2121,13 +2149,13 @@ WriteLiteral(" class=\"fa fa-envelope information\""); WriteLiteral("> Recipient Email Address:\r\n
\r\n"); - #line 736 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + #line 745 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" #line default #line hidden - #line 736 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + #line 745 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" using (Html.BeginForm(MVC.API.System.SendTestEmail(), FormMethod.Post)) { @@ -2152,33 +2180,33 @@ WriteLiteral(" name=\"Recipient\""); WriteLiteral(" type=\"text\""); -WriteAttribute("value", Tuple.Create(" value=\"", 34321), Tuple.Create("\"", 34354) +WriteAttribute("value", Tuple.Create(" value=\"", 34655), Tuple.Create("\"", 34688) - #line 739 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" - , Tuple.Create(Tuple.Create("", 34329), Tuple.Create(CurrentUser.EmailAddress + #line 748 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + , Tuple.Create(Tuple.Create("", 34663), Tuple.Create(CurrentUser.EmailAddress #line default #line hidden -, 34329), false) +, 34663), false) ); WriteLiteral(" />\r\n"); - #line 740 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + #line 749 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" #line default #line hidden - #line 740 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + #line 749 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" Write(Html.AntiForgeryToken()); #line default #line hidden - #line 740 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + #line 749 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" } @@ -2196,7 +2224,7 @@ WriteLiteral(@" var url = '"); - #line 750 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + #line 759 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" Write(Url.Action(MVC.API.System.UpdateEmailSettings())); @@ -2258,7 +2286,7 @@ WriteLiteral("\';\r\n var data = {\r\n " \r\n \r\n \r\n"); - #line 821 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + #line 830 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" } else { @@ -2283,7 +2311,7 @@ WriteLiteral(">\r\n SMTP Server:\r\n \r\n WriteLiteral(" "); - #line 832 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + #line 841 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" Write(Html.DisplayFor(m => m.EmailSmtpServer)); @@ -2299,7 +2327,7 @@ WriteLiteral(">\r\n Port:\r\n \r\n WriteLiteral(" "); - #line 840 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + #line 849 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" Write(Html.DisplayFor(m => m.EmailSmtpPort)); @@ -2316,7 +2344,7 @@ WriteLiteral(">\r\n Default From Address:\r\n WriteLiteral(" "); - #line 848 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + #line 857 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" Write(Html.DisplayFor(m => m.EmailFromAddress)); @@ -2327,13 +2355,30 @@ WriteLiteral("\r\n \r\n \r\n WriteLiteral(" style=\"width: 135px\""); +WriteLiteral(">\r\n Reply To Address:\r\n \r\n " + +"\r\n"); + +WriteLiteral(" "); + + + #line 865 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + Write(Html.DisplayFor(m => m.EmailReplyToAddress)); + + + #line default + #line hidden +WriteLiteral("\r\n \r\n \r\n \r\n \r\n Enable SSL:\r\n \r\n \r\n" + ""); WriteLiteral(" "); - #line 856 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + #line 873 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" Write(Html.CheckBoxFor(m => m.EmailEnableSsl, new { disabled = "disabled" })); @@ -2342,7 +2387,7 @@ WriteLiteral(" "); WriteLiteral(" "); - #line 856 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + #line 873 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" Write(Html.LabelFor(m => m.EmailEnableSsl)); @@ -2358,7 +2403,7 @@ WriteLiteral(">\r\n Username:\r\n \r\n WriteLiteral(" "); - #line 864 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + #line 881 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" Write(Html.DisplayFor(m => m.EmailUsername)); @@ -2374,7 +2419,7 @@ WriteLiteral(">\r\n Password:\r\n \r\n "table>\r\n \r\n"); - #line 877 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + #line 894 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" } @@ -2389,7 +2434,7 @@ WriteLiteral(">\r\n"); WriteLiteral(" "); - #line 879 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" + #line 896 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" Write(Html.ActionLinkButton("Update Device Last Network Logons", MVC.API.System.UpdateLastNetworkLogonDates())); diff --git a/Disco.Web/Extensions/T4MVC/API.SystemController.generated.cs b/Disco.Web/Extensions/T4MVC/API.SystemController.generated.cs index f01e8bad..496202f5 100644 --- a/Disco.Web/Extensions/T4MVC/API.SystemController.generated.cs +++ b/Disco.Web/Extensions/T4MVC/API.SystemController.generated.cs @@ -335,6 +335,7 @@ namespace Disco.Web.Areas.API.Controllers public readonly string SmtpServer = "SmtpServer"; public readonly string SmtpPort = "SmtpPort"; public readonly string FromAddress = "FromAddress"; + public readonly string ReplyToAddress = "ReplyToAddress"; public readonly string EnableSsl = "EnableSsl"; public readonly string Username = "Username"; public readonly string Password = "Password"; @@ -608,20 +609,21 @@ namespace Disco.Web.Areas.API.Controllers } [NonAction] - partial void UpdateEmailSettingsOverride(T4MVC_System_Web_Mvc_ActionResult callInfo, string SmtpServer, int? SmtpPort, string FromAddress, bool EnableSsl, string Username, string Password, bool redirect); + partial void UpdateEmailSettingsOverride(T4MVC_System_Web_Mvc_ActionResult callInfo, string SmtpServer, int? SmtpPort, string FromAddress, string ReplyToAddress, bool EnableSsl, string Username, string Password, bool redirect); [NonAction] - public override System.Web.Mvc.ActionResult UpdateEmailSettings(string SmtpServer, int? SmtpPort, string FromAddress, bool EnableSsl, string Username, string Password, bool redirect) + public override System.Web.Mvc.ActionResult UpdateEmailSettings(string SmtpServer, int? SmtpPort, string FromAddress, string ReplyToAddress, bool EnableSsl, string Username, string Password, bool redirect) { var callInfo = new T4MVC_System_Web_Mvc_ActionResult(Area, Name, ActionNames.UpdateEmailSettings); ModelUnbinderHelpers.AddRouteValues(callInfo.RouteValueDictionary, "SmtpServer", SmtpServer); ModelUnbinderHelpers.AddRouteValues(callInfo.RouteValueDictionary, "SmtpPort", SmtpPort); ModelUnbinderHelpers.AddRouteValues(callInfo.RouteValueDictionary, "FromAddress", FromAddress); + ModelUnbinderHelpers.AddRouteValues(callInfo.RouteValueDictionary, "ReplyToAddress", ReplyToAddress); ModelUnbinderHelpers.AddRouteValues(callInfo.RouteValueDictionary, "EnableSsl", EnableSsl); ModelUnbinderHelpers.AddRouteValues(callInfo.RouteValueDictionary, "Username", Username); ModelUnbinderHelpers.AddRouteValues(callInfo.RouteValueDictionary, "Password", Password); ModelUnbinderHelpers.AddRouteValues(callInfo.RouteValueDictionary, "redirect", redirect); - UpdateEmailSettingsOverride(callInfo, SmtpServer, SmtpPort, FromAddress, EnableSsl, Username, Password, redirect); + UpdateEmailSettingsOverride(callInfo, SmtpServer, SmtpPort, FromAddress, ReplyToAddress, EnableSsl, Username, Password, redirect); return callInfo; }