diff --git a/src/Ombi.Core.Tests/Senders/MassEmailSenderTests.cs b/src/Ombi.Core.Tests/Senders/MassEmailSenderTests.cs index c90797922..5c5cedabe 100644 --- a/src/Ombi.Core.Tests/Senders/MassEmailSenderTests.cs +++ b/src/Ombi.Core.Tests/Senders/MassEmailSenderTests.cs @@ -38,9 +38,9 @@ namespace Ombi.Core.Tests.Senders { Body = "Test", Subject = "Subject", - Users = new List + Users = new List { - new Store.Entities.OmbiUser + new OmbiUser { Id = "a" } @@ -143,5 +143,86 @@ namespace Ombi.Core.Tests.Senders _mocker.Verify(x => x.SendAdHoc(It.IsAny(), It.IsAny()), Times.Never); } + + [Test] + public async Task SendMassEmail_Bcc() + { + var model = new MassEmailModel + { + Body = "Test", + Subject = "Subject", + Bcc = true, + Users = new List + { + new OmbiUser + { + Id = "a" + }, + new OmbiUser + { + Id = "b" + } + } + }; + + _mocker.Setup>(x => x.Users).Returns(new List + { + new OmbiUser + { + Id = "a", + Email = "Test@test.com" + }, + new OmbiUser + { + Id = "b", + Email = "b@test.com" + } + }.AsQueryable().BuildMock().Object); + + var result = await _subject.SendMassEmail(model); + + _mocker.Verify(x => x.SendAdHoc(It.Is(m => m.Subject == model.Subject + && m.Message == model.Body + && m.Other["bcc"] == "Test@test.com,b@test.com"), It.IsAny()), Times.Once); + } + + [Test] + public async Task SendMassEmail_Bcc_NoEmails() + { + var model = new MassEmailModel + { + Body = "Test", + Subject = "Subject", + Bcc = true, + Users = new List + { + new OmbiUser + { + Id = "a" + }, + new OmbiUser + { + Id = "b" + } + } + }; + + _mocker.Setup>(x => x.Users).Returns(new List + { + new OmbiUser + { + Id = "a", + }, + new OmbiUser + { + Id = "b", + } + }.AsQueryable().BuildMock().Object); + + var result = await _subject.SendMassEmail(model); + + _mocker.Verify(x => x.SendAdHoc(It.IsAny(), It.IsAny()), Times.Never); + } + } } diff --git a/src/Ombi.Core/Models/MassEmailModel.cs b/src/Ombi.Core/Models/MassEmailModel.cs index ad09f0cb9..e175c0886 100644 --- a/src/Ombi.Core/Models/MassEmailModel.cs +++ b/src/Ombi.Core/Models/MassEmailModel.cs @@ -35,6 +35,8 @@ namespace Ombi.Core.Models public string Subject { get; set; } public string Body { get; set; } + public bool Bcc { get; set; } + public List Users { get; set; } } } \ No newline at end of file diff --git a/src/Ombi.Core/Senders/MassEmailSender.cs b/src/Ombi.Core/Senders/MassEmailSender.cs index 604224b34..106a63f49 100644 --- a/src/Ombi.Core/Senders/MassEmailSender.cs +++ b/src/Ombi.Core/Senders/MassEmailSender.cs @@ -25,7 +25,9 @@ // ************************************************************************/ #endregion +using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; @@ -64,6 +66,63 @@ namespace Ombi.Core.Senders var customization = await _customizationService.GetSettingsAsync(); var email = await _emailService.GetSettingsAsync(); var messagesSent = new List(); + if (model.Bcc) + { + await SendBccMails(model, customization, email, messagesSent); + } + else + { + await SendIndividualEmails(model, customization, email, messagesSent); + } + + await Task.WhenAll(messagesSent); + + return true; + } + + private async Task SendBccMails(MassEmailModel model, CustomizationSettings customization, EmailNotificationSettings email, List messagesSent) + { + var resolver = new NotificationMessageResolver(); + var curlys = new NotificationMessageCurlys(); + + var validUsers = new List(); + foreach (var user in model.Users) + { + var fullUser = await _userManager.Users.FirstOrDefaultAsync(x => x.Id == user.Id); + if (!fullUser.Email.HasValue()) + { + _log.LogInformation("User {0} has no email, cannot send mass email to this user", fullUser.UserName); + continue; + } + + validUsers.Add(fullUser); + } + + if (!validUsers.Any()) + { + return; + } + + var firstUser = validUsers.FirstOrDefault(); + + var bccAddress = string.Join(',', validUsers.Select(x => x.Email)); + curlys.Setup(firstUser, customization); + var template = new NotificationTemplates() { Message = model.Body, Subject = model.Subject }; + var content = resolver.ParseMessage(template, curlys); + var msg = new NotificationMessage + { + Message = content.Message, + Subject = content.Subject, + Other = new Dictionary { { "bcc", bccAddress } } + }; + + messagesSent.Add(_email.SendAdHoc(msg, email)); + } + + private async Task SendIndividualEmails(MassEmailModel model, CustomizationSettings customization, EmailNotificationSettings email, List messagesSent) + { + var resolver = new NotificationMessageResolver(); + var curlys = new NotificationMessageCurlys(); foreach (var user in model.Users) { var fullUser = await _userManager.Users.FirstOrDefaultAsync(x => x.Id == user.Id); @@ -72,8 +131,6 @@ namespace Ombi.Core.Senders _log.LogInformation("User {0} has no email, cannot send mass email to this user", fullUser.UserName); continue; } - var resolver = new NotificationMessageResolver(); - var curlys = new NotificationMessageCurlys(); curlys.Setup(fullUser, customization); var template = new NotificationTemplates() { Message = model.Body, Subject = model.Subject }; var content = resolver.ParseMessage(template, curlys); @@ -83,13 +140,19 @@ namespace Ombi.Core.Senders To = fullUser.Email, Subject = content.Subject }; - messagesSent.Add(_email.SendAdHoc(msg, email)); + messagesSent.Add(DelayEmail(msg, email)); _log.LogInformation("Sent mass email to user {0} @ {1}", fullUser.UserName, fullUser.Email); } + } - await Task.WhenAll(messagesSent); - - return true; + /// + /// This will add a 2 second delay, this is to help with concurrent connection limits + /// + /// + private async Task DelayEmail(NotificationMessage msg, EmailNotificationSettings email) + { + await Task.Delay(2000); + await _email.SendAdHoc(msg, email); } } } \ No newline at end of file diff --git a/src/Ombi.Notifications/GenericEmailProvider.cs b/src/Ombi.Notifications/GenericEmailProvider.cs index 15f17af92..fdeb1f49c 100644 --- a/src/Ombi.Notifications/GenericEmailProvider.cs +++ b/src/Ombi.Notifications/GenericEmailProvider.cs @@ -64,7 +64,20 @@ namespace Ombi.Notifications MessageId = messageId }; message.From.Add(new MailboxAddress(string.IsNullOrEmpty(settings.SenderName) ? settings.SenderAddress : settings.SenderName, settings.SenderAddress)); - message.To.Add(new MailboxAddress(model.To, model.To)); + if (model.To.HasValue()) + { + message.To.Add(new MailboxAddress(model.To, model.To)); + } + + // Check for BCC + if (model.Other.TryGetValue("bcc", out var bcc)) + { + var bccList = bcc.Split(',', StringSplitOptions.RemoveEmptyEntries); + foreach (var item in bccList) + { + message.Bcc.Add(new MailboxAddress(item, item)); + } + } using (var client = new SmtpClient()) { diff --git a/src/Ombi/.vscode/settings.json b/src/Ombi/.vscode/settings.json index 89d2dbd93..fd36c353d 100644 --- a/src/Ombi/.vscode/settings.json +++ b/src/Ombi/.vscode/settings.json @@ -10,13 +10,13 @@ "cSpell.words": [ "usermanagement" ], - "discord.enabled": true, "conventionalCommits.scopes": [ "discover", "request-limits", "notifications", "settings", "user-management", - "newsletter" + "newsletter", + "mass-email" ] } diff --git a/src/Ombi/ClientApp/src/app/interfaces/IUser.ts b/src/Ombi/ClientApp/src/app/interfaces/IUser.ts index 2db5a5de2..97882e783 100644 --- a/src/Ombi/ClientApp/src/app/interfaces/IUser.ts +++ b/src/Ombi/ClientApp/src/app/interfaces/IUser.ts @@ -121,6 +121,7 @@ export interface IMassEmailModel { subject: string; body: string; users: IUser[]; + bcc: boolean; } export interface INotificationPreferences { diff --git a/src/Ombi/ClientApp/src/app/settings/massemail/massemail.component.html b/src/Ombi/ClientApp/src/app/settings/massemail/massemail.component.html index 49890ac6e..fb6ab0ad0 100644 --- a/src/Ombi/ClientApp/src/app/settings/massemail/massemail.component.html +++ b/src/Ombi/ClientApp/src/app/settings/massemail/massemail.component.html @@ -3,15 +3,15 @@
Mass Email - +
Hey! We need a subject!
-
- +
+
@@ -20,7 +20,14 @@ May appear differently on email clients
+
+ This will send out the Mass email BCC'ing all of the selected users rather than sending individual messages +
+ BCC +
+
+
@@ -28,23 +35,21 @@
- - + +
-
- - -
+
+ Select All +
-
- - -
+
+ {{u.user.userName}} ({{u.user.emailAddress}}) +
- +
diff --git a/src/Ombi/ClientApp/src/app/settings/massemail/massemail.component.ts b/src/Ombi/ClientApp/src/app/settings/massemail/massemail.component.ts index 17beb5405..1ed2fe995 100644 --- a/src/Ombi/ClientApp/src/app/settings/massemail/massemail.component.ts +++ b/src/Ombi/ClientApp/src/app/settings/massemail/massemail.component.ts @@ -12,6 +12,7 @@ export class MassEmailComponent implements OnInit { public users: IMassEmailUserModel[] = []; public message: string; public subject: string; + public bcc: boolean; public missingSubject = false; @@ -26,17 +27,19 @@ export class MassEmailComponent implements OnInit { public ngOnInit(): void { this.identityService.getUsers().subscribe(x => { x.forEach(u => { - this.users.push({ - user: u, - selected: false, - }); + if (u.emailAddress) { + this.users.push({ + user: u, + selected: false, + }); + } }); }); this.settingsService.getEmailSettingsEnabled().subscribe(x => this.emailEnabled = x); } - public selectAllUsers() { - this.users.forEach(u => u.selected = !u.selected); + public selectAllUsers(event: any) { + this.users.forEach(u => u.selected = event.checked); } public send() { @@ -44,10 +47,10 @@ export class MassEmailComponent implements OnInit { this.missingSubject = true; return; } - if(!this.emailEnabled) { - this.notification.error("You have not yet setup your email notifications, do that first!"); - return; - } + // if(!this.emailEnabled) { + // this.notification.error("You have not yet setup your email notifications, do that first!"); + // return; + // } this.missingSubject = false; // Where(x => x.selected).Select(x => x.user) const selectedUsers = this.users.filter(u => { @@ -63,6 +66,7 @@ export class MassEmailComponent implements OnInit { users: selectedUsers, subject: this.subject, body: this.message, + bcc: this.bcc, }; this.notification.info("Sending","Sending mass email... Please wait"); this.notificationMessageService.sendMassEmail(model).subscribe(x => {