diff --git a/Ombi.Core/Ombi.Core.csproj b/Ombi.Core/Ombi.Core.csproj index 9866e6fb0..f6562c2fc 100644 --- a/Ombi.Core/Ombi.Core.csproj +++ b/Ombi.Core/Ombi.Core.csproj @@ -124,6 +124,7 @@ + diff --git a/Ombi.Core/SettingModels/MassEmailSettings.cs b/Ombi.Core/SettingModels/MassEmailSettings.cs new file mode 100644 index 000000000..0be28a92d --- /dev/null +++ b/Ombi.Core/SettingModels/MassEmailSettings.cs @@ -0,0 +1,35 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: EmailNotificationSettings.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +namespace Ombi.Core.SettingModels +{ + public sealed class MassEmailSettings : NotificationSettings + { + public string Users { get; set; } + public string Subject { get; set; } + public string Body { get; set; } + } +} \ No newline at end of file diff --git a/Ombi.Services/Interfaces/IMassEmail.cs b/Ombi.Services/Interfaces/IMassEmail.cs new file mode 100644 index 000000000..9751cc870 --- /dev/null +++ b/Ombi.Services/Interfaces/IMassEmail.cs @@ -0,0 +1,12 @@ +using Quartz; + +namespace Ombi.Services.Jobs +{ + public interface IMassEmail + { + void Execute(IJobExecutionContext context); + void MassEmailAdminTest(string html, string subject); + void SendMassEmail(string html, string subject); + + } +} \ No newline at end of file diff --git a/Ombi.Services/Interfaces/IRecentlyAdded.cs b/Ombi.Services/Interfaces/IRecentlyAdded.cs index 09a7220f5..c18ca8e27 100644 --- a/Ombi.Services/Interfaces/IRecentlyAdded.cs +++ b/Ombi.Services/Interfaces/IRecentlyAdded.cs @@ -5,7 +5,7 @@ namespace Ombi.Services.Jobs public interface IRecentlyAdded { void Execute(IJobExecutionContext context); - void Test(); + void RecentlyAddedAdminTest(); void Start(); } } \ No newline at end of file diff --git a/Ombi.Services/Jobs/RecentlyAdded.cs b/Ombi.Services/Jobs/RecentlyAdded.cs index 563dd35a5..9ee52f231 100644 --- a/Ombi.Services/Jobs/RecentlyAdded.cs +++ b/Ombi.Services/Jobs/RecentlyAdded.cs @@ -48,7 +48,7 @@ using Quartz; namespace Ombi.Services.Jobs { - public class RecentlyAdded : HtmlTemplateGenerator, IJob, IRecentlyAdded + public class RecentlyAdded : HtmlTemplateGenerator, IJob, IRecentlyAdded, IMassEmail { public RecentlyAdded(IPlexApi api, ISettingsService plexSettings, ISettingsService email, IJobRecord rec, @@ -105,12 +105,30 @@ namespace Ombi.Services.Jobs Start(); } - public void Test() + public void RecentlyAddedAdminTest() { - Log.Debug("Starting Test Newsletter"); + Log.Debug("Starting Recently Added Newsletter Test"); var settings = NewsletterSettings.GetSettings(); Start(settings, true); } + public void MassEmailAdminTest(string html, string subject) + { + Log.Debug("Starting Mass Email Test"); + var settings = NewsletterSettings.GetSettings(); + var plexSettings = PlexSettings.GetSettings(); + var template = new MassEmailTemplate(); + var body = template.LoadTemplate(html); + Send(settings, body, plexSettings, true, subject); + } + public void SendMassEmail(string html, string subject) + { + Log.Debug("Starting Mass Email Test"); + var settings = NewsletterSettings.GetSettings(); + var plexSettings = PlexSettings.GetSettings(); + var template = new MassEmailTemplate(); + var body = template.LoadTemplate(html); + Send(settings, body, plexSettings, false, subject); + } private void Start(NewletterSettings newletterSettings, bool testEmail = false) { @@ -439,7 +457,7 @@ namespace Ombi.Services.Jobs sb.Append("

"); } - private void Send(NewletterSettings newletterSettings, string html, PlexSettings plexSettings, bool testEmail = false) + private void Send(NewletterSettings newletterSettings, string html, PlexSettings plexSettings, bool testEmail = false, string subject = "New Content on Plex!") { Log.Debug("Entering Send"); var settings = EmailSettings.GetSettings(); @@ -454,7 +472,7 @@ namespace Ombi.Services.Jobs var message = new MimeMessage { Body = body.ToMessageBody(), - Subject = "New Content on Plex!", + Subject = subject }; Log.Debug("Created Plain/HTML MIME body"); diff --git a/Ombi.Services/Jobs/Templates/MassEmailTemplate.cs b/Ombi.Services/Jobs/Templates/MassEmailTemplate.cs new file mode 100644 index 000000000..ea73c027d --- /dev/null +++ b/Ombi.Services/Jobs/Templates/MassEmailTemplate.cs @@ -0,0 +1,58 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: RecentlyAddedTemplate.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion + +using System; +using System.IO; +using System.Text; +using System.Windows.Forms; +using NLog; + +namespace Ombi.Services.Jobs.Templates +{ + public class MassEmailTemplate + { + public string TemplateLocation => Path.Combine(Path.GetDirectoryName(Application.ExecutablePath) ?? string.Empty, "Jobs", "Templates", "MassEmailTemplate.html"); + private static readonly Logger Log = LogManager.GetCurrentClassLogger(); + + private const string RecentlyAddedKey = "{@MASSEMAIL}"; + + public string LoadTemplate(string html) + { + try + { + var sb = new StringBuilder(File.ReadAllText(TemplateLocation)); + sb.Replace(RecentlyAddedKey, html); + return sb.ToString(); + } + catch (Exception e) + { + Log.Error(e); + return string.Empty; + } + } + } +} \ No newline at end of file diff --git a/Ombi.Services/Jobs/Templates/MassEmailTemplate.html b/Ombi.Services/Jobs/Templates/MassEmailTemplate.html new file mode 100644 index 000000000..02214c6af --- /dev/null +++ b/Ombi.Services/Jobs/Templates/MassEmailTemplate.html @@ -0,0 +1,181 @@ + + + + + + Ombi + + + + + + + + + +
  +
+ + + + + + + + + + + +
+ + + + + + + +
+ +
+ {@MASSEMAIL} +
+
+ + + + + + +
+
 
+ + \ No newline at end of file diff --git a/Ombi.Services/Ombi.Services.csproj b/Ombi.Services/Ombi.Services.csproj index 1735094dd..a37279f7d 100644 --- a/Ombi.Services/Ombi.Services.csproj +++ b/Ombi.Services/Ombi.Services.csproj @@ -87,6 +87,7 @@ + @@ -107,6 +108,7 @@ + @@ -180,6 +182,9 @@ + + PreserveNewest + PreserveNewest diff --git a/Ombi.UI/Modules/Admin/AdminModule.cs b/Ombi.UI/Modules/Admin/AdminModule.cs index a83ff8bd1..a0b6216f7 100644 --- a/Ombi.UI/Modules/Admin/AdminModule.cs +++ b/Ombi.UI/Modules/Admin/AdminModule.cs @@ -92,6 +92,7 @@ namespace Ombi.UI.Modules.Admin private IJobRecord JobRecorder { get; } private IAnalytics Analytics { get; } private IRecentlyAdded RecentlyAdded { get; } + private IMassEmail MassEmail { get; } private ISettingsService NotifySettings { get; } private ISettingsService DiscordSettings { get; } private IDiscordApi DiscordApi { get; } @@ -123,7 +124,7 @@ namespace Ombi.UI.Modules.Admin ICacheProvider cache, ISettingsService slackSettings, ISlackApi slackApi, ISettingsService lp, ISettingsService scheduler, IJobRecord rec, IAnalytics analytics, - ISettingsService notifyService, IRecentlyAdded recentlyAdded, + ISettingsService notifyService, IRecentlyAdded recentlyAdded, IMassEmail massEmail, ISettingsService watcherSettings , ISettingsService discord, IDiscordApi discordapi, ISettingsService settings, IRadarrApi radarrApi, @@ -158,6 +159,7 @@ namespace Ombi.UI.Modules.Admin Analytics = analytics; NotifySettings = notifyService; RecentlyAdded = recentlyAdded; + MassEmail = massEmail; WatcherSettings = watcherSettings; DiscordSettings = discord; DiscordApi = discordapi; @@ -222,6 +224,9 @@ namespace Ombi.UI.Modules.Admin Get["/newsletter", true] = async (x, ct) => await Newsletter(); Post["/newsletter", true] = async (x, ct) => await SaveNewsletter(); + Post["/testnewsletteradminemail"] = x => TestNewsletterAdminEmail(); + Post["/testmassadminemail"] = x => TestMassAdminEmail(); + Post["/sendmassemail"] = x => SendMassEmail(); Post["/createapikey"] = x => CreateApiKey(); @@ -246,7 +251,6 @@ namespace Ombi.UI.Modules.Admin Get["/notificationsettings", true] = async (x, ct) => await NotificationSettings(); Post["/notificationsettings"] = x => SaveNotificationSettings(); - Post["/recentlyAddedTest"] = x => RecentlyAddedTest(); } private async Task Authentication() @@ -1229,13 +1233,13 @@ namespace Ombi.UI.Modules.Admin var model = this.Bind(); return View["NotificationSettings", model]; } - - private Response RecentlyAddedTest() + + private Response TestNewsletterAdminEmail() { try { - Log.Debug("Clicked TEST"); - RecentlyAdded.Test(); + Log.Debug("Clicked Admin Newsletter Email Test"); + RecentlyAdded.RecentlyAddedAdminTest(); return Response.AsJson(new JsonResponseModel { Result = true, Message = "Sent email to administrator" }); } catch (Exception e) @@ -1244,5 +1248,50 @@ namespace Ombi.UI.Modules.Admin return Response.AsJson(new JsonResponseModel { Result = false, Message = e.Message }); } } + private Response TestMassAdminEmail() + { + try + { + var settings = this.Bind(); + Log.Debug("Clicked Admin Mass Email Test"); + if (settings.Subject == null) { + return Response.AsJson(new JsonResponseModel { Result = false, Message = "Please Set a Subject" }); + } + if (settings.Body == null) + { + return Response.AsJson(new JsonResponseModel { Result = false, Message = "Please Set a Body" }); + } + MassEmail.MassEmailAdminTest(settings.Body.Replace("\n", "
"), settings.Subject); + return Response.AsJson(new JsonResponseModel { Result = true, Message = "Sent email to administrator" }); + } + catch (Exception e) + { + Log.Error(e); + return Response.AsJson(new JsonResponseModel { Result = false, Message = e.Message }); + } + } + private Response SendMassEmail() + { + try + { + var settings = this.Bind(); + Log.Debug("Clicked Admin Mass Email Test"); + if (settings.Subject == null) + { + return Response.AsJson(new JsonResponseModel { Result = false, Message = "Please Set a Subject" }); + } + if (settings.Body == null) + { + return Response.AsJson(new JsonResponseModel { Result = false, Message = "Please Set a Body" }); + } + MassEmail.SendMassEmail(settings.Body.Replace("\n", "
"), settings.Subject); + return Response.AsJson(new JsonResponseModel { Result = true, Message = "Sent email to All users" }); + } + catch (Exception e) + { + Log.Error(e); + return Response.AsJson(new JsonResponseModel { Result = false, Message = e.Message }); + } + } } } \ No newline at end of file diff --git a/Ombi.UI/NinjectModules/ServicesModule.cs b/Ombi.UI/NinjectModules/ServicesModule.cs index 95dfcf433..95fbe4ba5 100644 --- a/Ombi.UI/NinjectModules/ServicesModule.cs +++ b/Ombi.UI/NinjectModules/ServicesModule.cs @@ -49,6 +49,7 @@ namespace Ombi.UI.NinjectModules Bind().To(); Bind().To(); Bind().To(); + Bind().To(); Bind().To(); Bind().To(); Bind().To(); diff --git a/Ombi.UI/Views/Admin/NewsletterSettings.cshtml b/Ombi.UI/Views/Admin/NewsletterSettings.cshtml index 898a7a761..2b9f52838 100644 --- a/Ombi.UI/Views/Admin/NewsletterSettings.cshtml +++ b/Ombi.UI/Views/Admin/NewsletterSettings.cshtml @@ -7,46 +7,48 @@
Newsletter Settings +
- -
-
+ +
+
- Note: This will require you to setup your email notifications -
- @if (Model.SendRecentlyAddedEmail) - { - - } - else - { - - } + Note: This will require you to setup your email notifications +
+ @if (Model.SendRecentlyAddedEmail) + { + + } + else + { + + } +
-
-
+
-
- - You can add multiple email addresses by using the ; delimiter -
- +
+ + You can add multiple email addresses by using the ; delimiter +
+ +
-
-
-
- +
+
+ +
-
-
-
-
-
- +
+
+
+
+ +
@@ -54,6 +56,39 @@
+
+
+ Mass Email +
+
+ Note: This will require you to setup your email notifications +
+
+ +
+ +
+
+
+ + + + Supports HTML + +
+
+
+ +
+
+
+
+ +
+
+
+
+
@@ -90,8 +125,8 @@ $('#recentlyAddedBtn').click(function (e) { e.preventDefault(); var base = '@Html.GetBaseUrl()'; - var url = createBaseUrl(base, '/admin/recentlyAddedTest'); - $('#spinner').attr("class", "fa fa-spinner fa-spin"); + var url = createBaseUrl(base, '/admin/testnewsletteradminemail'); + $('#testEmailSpinner').attr("class", "fa fa-spinner fa-spin"); $.ajax({ type: "post", url: url, @@ -99,17 +134,74 @@ success: function (response) { if (response) { generateNotify(response.message, "success"); - $('#spinner').attr("class", "fa fa-check"); + $('#testSendMassEmailSpinner').attr("class", "fa fa-check"); } else { generateNotify(response.message, "danger"); - $('#spinner').attr("class", "fa fa-times"); + $('#testSendMassEmailSpinner').attr("class", "fa fa-times"); } }, error: function (e) { console.log(e); generateNotify("Something went wrong!", "danger"); - $('#spinner').attr("class", "fa fa-times"); + $('#testSendMassEmailSpinner').attr("class", "fa fa-times"); + } + }); + }); + + $('#testSendMassEmailBtn').click(function (e) { + e.preventDefault(); + var base = '@Html.GetBaseUrl()'; + var url = createBaseUrl(base, '/admin/testmassadminemail'); + $('#testSendMassEmailSpinner').attr("class", "fa fa-spinner fa-spin"); + var data = { "Users": "", "Body": $("#massEmailBody").val(), "Subject": $("#massEmailSubject").val() }; + $.ajax({ + type: "post", + url: url, + data: data, + dataType: "json", + success: function (response) { + if (response.result) { + generateNotify(response.message, "success"); + $('#testSendMassEmailSpinner').attr("class", "fa fa-check"); + } else { + + generateNotify(response.message, "danger"); + $('#testSendMassEmailSpinner').attr("class", "fa fa-times"); + } + }, + error: function (e) { + console.log(e); + generateNotify("Something went wrong!", "danger"); + $('#testSendMassEmailSpinner').attr("class", "fa fa-times"); + } + }); + }); + $('#sendMassEmailBtn').click(function (e) { + e.preventDefault(); + var base = '@Html.GetBaseUrl()'; + var url = createBaseUrl(base, '/admin/sendmassemail'); + $('#sendMassEmailSpinner').attr("class", "fa fa-spinner fa-spin"); + var data = { "Users": "", "Body": $("#massEmailBody").val(), "Subject": $("#massEmailSubject").val() }; + $.ajax({ + type: "post", + url: url, + data: data, + dataType: "json", + success: function (response) { + if (response.result) { + generateNotify(response.message, "success"); + $('#sendMassEmailSpinner').attr("class", "fa fa-check"); + } else { + + generateNotify(response.message, "danger"); + $('#sendMassEmailSpinner').attr("class", "fa fa-times"); + } + }, + error: function (e) { + console.log(e); + generateNotify("Something went wrong!", "danger"); + $('#sendMassEmailSpinner').attr("class", "fa fa-times"); } }); });