From f8485a6275e73bad7271de571c9af6ab05a255f2 Mon Sep 17 00:00:00 2001 From: AdrianHarwood Date: Thu, 28 May 2026 21:00:58 +0100 Subject: [PATCH 1/6] Changed send email to just focus on one task, one email. --- PPMTool/Services/EmailService.cs | 37 +++++++++++++++++++------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/PPMTool/Services/EmailService.cs b/PPMTool/Services/EmailService.cs index 730f8208..08c89dcc 100644 --- a/PPMTool/Services/EmailService.cs +++ b/PPMTool/Services/EmailService.cs @@ -43,12 +43,12 @@ ILogger logger public ILogger Logger { get; } /// - /// Send an email to the list of recipients provided. + /// Send an email to the recipient provided. /// /// /// /// - public void SendEmail(IEnumerable to, string subject, string message) + public void SendEmail(string to, string subject, string message) { var mailMessage = new MailMessage @@ -58,13 +58,9 @@ public void SendEmail(IEnumerable to, string subject, string message) Body = message, IsBodyHtml = true, }; + mailMessage.To.Add(to); - foreach (var recipient in to.Distinct()) - { - mailMessage.To.Add(recipient); - } - - Logger.LogInformation($"Sending email to {string.Join(',', mailMessage.To)}, subject {mailMessage.Subject}"); + Logger.LogInformation($"Sending email to {mailMessage.To}, subject {mailMessage.Subject}"); #if RELEASE // Launch a background task to do the sending @@ -78,7 +74,7 @@ public void SendEmail(IEnumerable to, string subject, string message) } catch (Exception e) { - Logger.LogError($"Failed to send email to {string.Join(',', mailMessage.To)}, subject {mailMessage.Subject}:\n{e}"); + Logger.LogError($"Failed to send email to {mailMessage.To}, subject {mailMessage.Subject}:\n{e}"); } }); #endif @@ -94,7 +90,7 @@ public async Task SendTimesheetSubmissionEmailNotificationAsync(Person staff, Ti List recipients = new List(); // Run a background thread to do the sending and updating - await Task.Run(() => + await Task.Run(async () => { try { @@ -128,7 +124,11 @@ await Task.Run(() => // Send email Debug.WriteLine($"** Sending Timesheet Submission email to {string.Join("|", recipients)}"); - SendEmail(recipients, subject, body.ToString()); + foreach (var recipient in recipients) + { + SendEmail(recipient, subject, body.ToString()); + await Task.Delay(1000); + } } } } @@ -310,9 +310,13 @@ await Task.Run(async () => } recipients.AddRange(lineManagerEmailAddresses); } + Debug.WriteLine($"** Sending email to {string.Join(',', recipients)}"); - SendEmail(recipients, subject, body.ToString()); - await Task.Delay(1000); + foreach (var recipient in recipients) + { + SendEmail(recipient, subject, body.ToString()); + await Task.Delay(1000); + } } } } @@ -470,8 +474,11 @@ await Task.Run(async () => recipients.AddRange(lineManagerEmailAddresses); } Debug.WriteLine($"** Sending email to {string.Join(',', recipients)}"); - SendEmail(recipients, subject, body.ToString()); - await Task.Delay(1000); + foreach (var recipient in recipients) + { + SendEmail(recipient, subject, body.ToString()); + await Task.Delay(1000); + } } } } From 57c1b5c043d7445b18091b26702da5f97f531544 Mon Sep 17 00:00:00 2001 From: PhilBradbury Date: Mon, 8 Jun 2026 15:32:51 +0100 Subject: [PATCH 2/6] Removed creating an email address using username@manchester.ac.uk if none already registered. If no email then don't send anything. --- PPMTool/Services/EmailService.cs | 46 +++++++++++++++++--------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/PPMTool/Services/EmailService.cs b/PPMTool/Services/EmailService.cs index 08c89dcc..8bbab55b 100644 --- a/PPMTool/Services/EmailService.cs +++ b/PPMTool/Services/EmailService.cs @@ -99,35 +99,37 @@ await Task.Run(async () => { Person lineManager = staff.LineManager; - if (lineManager != staff) // No point in AH emailing himself about his timesheet. :) + if (lineManager != staff) // No point in Superuser emailing themselves about their timesheet. :) { User lineManagerUser = UserService.GetAll(context).First(p => p.Person.PersonId == lineManager.PersonId); var lineManagerEmailAddresses = lineManagerUser.GetNormalisedEmailAddresses(); - if (!lineManagerEmailAddresses.Any()) + if (lineManagerEmailAddresses.Any()) { - lineManagerEmailAddresses.Add($"{lineManagerUser.CASUserName}@manchester.ac.uk"); - } - foreach (var lineManagerEmailAddress in lineManagerEmailAddresses) - { - recipients.Add(lineManagerEmailAddress); + foreach (var lineManagerEmailAddress in lineManagerEmailAddresses) + { + recipients.Add(lineManagerEmailAddress); + } } - // Create email - var subject = $"{Configuration["Email:TimesheetSubmissionEmailSubject"]}. {staff.ShortName} [{timesheet.StartDate.ToString("dd/MM/yy")}]"; - - StringBuilder body = new StringBuilder(); - body.Append($"

Dear {lineManager.Name},

"); - body.Append($"

{Configuration["Email:TimesheetSubmissionEmailBody"]} by {staff.Name} for the week commencing {timesheet.StartDate.ToString("dd/MM/yy")}.

"); - body.Append($"

{Configuration["Email:TimesheetSubmissionEmailEndBody"]}

"); - body.Append($"

Review this timesheet on CapX

"); - body.Append("

Sent from CapX

"); - - // Send email - Debug.WriteLine($"** Sending Timesheet Submission email to {string.Join("|", recipients)}"); - foreach (var recipient in recipients) + if (recipients.Any()) // Build the email and send it if there are any email addresses { - SendEmail(recipient, subject, body.ToString()); - await Task.Delay(1000); + // Create email + var subject = $"{Configuration["Email:TimesheetSubmissionEmailSubject"]}. {staff.ShortName} [{timesheet.StartDate.ToString("dd/MM/yy")}]"; + + StringBuilder body = new StringBuilder(); + body.Append($"

Dear {lineManager.Name},

"); + body.Append($"

{Configuration["Email:TimesheetSubmissionEmailBody"]} by {staff.Name} for the week commencing {timesheet.StartDate.ToString("dd/MM/yy")}.

"); + body.Append($"

{Configuration["Email:TimesheetSubmissionEmailEndBody"]}

"); + body.Append($"

Review this timesheet on CapX

"); + body.Append("

Sent from CapX

"); + + // Send email + Debug.WriteLine($"** Sending Timesheet Submission email to {string.Join("|", recipients)}"); + foreach (var recipient in recipients) + { + SendEmail(recipient, subject, body.ToString()); + await Task.Delay(1000); + } } } } From ac43e482dd1559aa5578dd06331fa9e4e4c848ef Mon Sep 17 00:00:00 2001 From: Adrian Harwood Date: Tue, 9 Jun 2026 12:06:47 +0100 Subject: [PATCH 3/6] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- PPMTool/Services/EmailService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PPMTool/Services/EmailService.cs b/PPMTool/Services/EmailService.cs index 8bbab55b..bd2ccfba 100644 --- a/PPMTool/Services/EmailService.cs +++ b/PPMTool/Services/EmailService.cs @@ -99,7 +99,7 @@ await Task.Run(async () => { Person lineManager = staff.LineManager; - if (lineManager != staff) // No point in Superuser emailing themselves about their timesheet. :) +if (lineManager != staff) // No point emailing someone about their own timesheet if they are their own line manager. :) { User lineManagerUser = UserService.GetAll(context).First(p => p.Person.PersonId == lineManager.PersonId); var lineManagerEmailAddresses = lineManagerUser.GetNormalisedEmailAddresses(); From 3024f15f7dfe827ae22b118943c8e9dff20a25c6 Mon Sep 17 00:00:00 2001 From: Adrian Harwood Date: Tue, 9 Jun 2026 12:07:17 +0100 Subject: [PATCH 4/6] Potential fix for pull request finding Logging fix Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- PPMTool/Services/EmailService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PPMTool/Services/EmailService.cs b/PPMTool/Services/EmailService.cs index bd2ccfba..6a94915b 100644 --- a/PPMTool/Services/EmailService.cs +++ b/PPMTool/Services/EmailService.cs @@ -60,7 +60,7 @@ public void SendEmail(string to, string subject, string message) }; mailMessage.To.Add(to); - Logger.LogInformation($"Sending email to {mailMessage.To}, subject {mailMessage.Subject}"); +Logger.LogInformation("Sending email to {To}, subject {Subject}", to, mailMessage.Subject); #if RELEASE // Launch a background task to do the sending From 5af5bc76b904ec44fb05fce0ba770885db74288d Mon Sep 17 00:00:00 2001 From: Adrian Harwood Date: Tue, 9 Jun 2026 12:07:30 +0100 Subject: [PATCH 5/6] Potential fix for pull request finding Logging fix Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- PPMTool/Services/EmailService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PPMTool/Services/EmailService.cs b/PPMTool/Services/EmailService.cs index 6a94915b..8cb32851 100644 --- a/PPMTool/Services/EmailService.cs +++ b/PPMTool/Services/EmailService.cs @@ -74,7 +74,7 @@ public void SendEmail(string to, string subject, string message) } catch (Exception e) { - Logger.LogError($"Failed to send email to {mailMessage.To}, subject {mailMessage.Subject}:\n{e}"); +Logger.LogError(e, "Failed to send email to {To}, subject {Subject}", to, mailMessage.Subject); } }); #endif From 028e03030c6790485bd38f868ae0ecfe4bdd9246 Mon Sep 17 00:00:00 2001 From: AdrianHarwood Date: Tue, 9 Jun 2026 12:13:45 +0100 Subject: [PATCH 6/6] Fixed formatting --- PPMTool/Services/EmailService.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/PPMTool/Services/EmailService.cs b/PPMTool/Services/EmailService.cs index 8cb32851..c57a3833 100644 --- a/PPMTool/Services/EmailService.cs +++ b/PPMTool/Services/EmailService.cs @@ -50,7 +50,6 @@ ILogger logger /// public void SendEmail(string to, string subject, string message) { - var mailMessage = new MailMessage { From = new MailAddress(Configuration["Email:From"]), @@ -60,7 +59,7 @@ public void SendEmail(string to, string subject, string message) }; mailMessage.To.Add(to); -Logger.LogInformation("Sending email to {To}, subject {Subject}", to, mailMessage.Subject); + Logger.LogInformation($"Sending email to {to}, subject {mailMessage.Subject}"); #if RELEASE // Launch a background task to do the sending @@ -74,7 +73,7 @@ public void SendEmail(string to, string subject, string message) } catch (Exception e) { -Logger.LogError(e, "Failed to send email to {To}, subject {Subject}", to, mailMessage.Subject); + Logger.LogInformation($"Failed to send email to {to}, subject {mailMessage.Subject}"); } }); #endif @@ -99,7 +98,7 @@ await Task.Run(async () => { Person lineManager = staff.LineManager; -if (lineManager != staff) // No point emailing someone about their own timesheet if they are their own line manager. :) + if (lineManager != staff) // No point emailing someone about their own timesheet if they are their own line manager. :) { User lineManagerUser = UserService.GetAll(context).First(p => p.Person.PersonId == lineManager.PersonId); var lineManagerEmailAddresses = lineManagerUser.GetNormalisedEmailAddresses(); @@ -111,7 +110,8 @@ await Task.Run(async () => } } - if (recipients.Any()) // Build the email and send it if there are any email addresses + // Only build the email and send it if there are any email addresses + if (recipients.Any()) { // Create email var subject = $"{Configuration["Email:TimesheetSubmissionEmailSubject"]}. {staff.ShortName} [{timesheet.StartDate.ToString("dd/MM/yy")}]";