Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 43 additions & 34 deletions PPMTool/Services/EmailService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,23 @@ ILogger logger
public ILogger Logger { get; }

/// <summary>
/// Send an email to the list of recipients provided.
/// Send an email to the recipient provided.
/// </summary>
/// <param name="to"></param>
/// <param name="subject"></param>
/// <param name="message"></param>
public void SendEmail(IEnumerable<string> to, string subject, string message)
public void SendEmail(string to, string subject, string message)
{

var mailMessage = new MailMessage
{
From = new MailAddress(Configuration["Email:From"]),
Subject = subject,
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 {to}, subject {mailMessage.Subject}");

#if RELEASE
// Launch a background task to do the sending
Expand All @@ -78,7 +73,7 @@ public void SendEmail(IEnumerable<string> 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.LogInformation($"Failed to send email to {to}, subject {mailMessage.Subject}");
}
});
#endif
Expand All @@ -94,7 +89,7 @@ public async Task SendTimesheetSubmissionEmailNotificationAsync(Person staff, Ti
List<string> recipients = new List<string>();

// Run a background thread to do the sending and updating
await Task.Run(() =>
await Task.Run(async () =>
{
try
{
Expand All @@ -103,32 +98,39 @@ await Task.Run(() =>
{
Person lineManager = staff.LineManager;

if (lineManager != staff) // No point in AH emailing himself about his 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();
if (!lineManagerEmailAddresses.Any())
if (lineManagerEmailAddresses.Any())
{
lineManagerEmailAddresses.Add($"{lineManagerUser.CASUserName}@manchester.ac.uk");
foreach (var lineManagerEmailAddress in lineManagerEmailAddresses)
{
Comment thread
PhilBradbury marked this conversation as resolved.
recipients.Add(lineManagerEmailAddress);
}
}
foreach (var lineManagerEmailAddress in lineManagerEmailAddresses)

// Only build the email and send it if there are any email addresses
if (recipients.Any())
{
recipients.Add(lineManagerEmailAddress);
// Create email
var subject = $"{Configuration["Email:TimesheetSubmissionEmailSubject"]}. {staff.ShortName} [{timesheet.StartDate.ToString("dd/MM/yy")}]";

StringBuilder body = new StringBuilder();
body.Append($"<p>Dear {lineManager.Name},</p>");
body.Append($"<p>{Configuration["Email:TimesheetSubmissionEmailBody"]} by {staff.Name} for the week commencing {timesheet.StartDate.ToString("dd/MM/yy")}.</p>");
body.Append($"<p>{Configuration["Email:TimesheetSubmissionEmailEndBody"]}</p>");
body.Append($"<p><a href=\"{Configuration["Authentication:HostUrl"]}/timesheets/addtimesheet/{timesheet.TimesheetId.ToString()}\">Review this timesheet on CapX</a></p>");
body.Append("<p><em>Sent from CapX</em></p>");

// 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);
}
}

// Create email
var subject = $"{Configuration["Email:TimesheetSubmissionEmailSubject"]}. {staff.ShortName} [{timesheet.StartDate.ToString("dd/MM/yy")}]";

StringBuilder body = new StringBuilder();
body.Append($"<p>Dear {lineManager.Name},</p>");
body.Append($"<p>{Configuration["Email:TimesheetSubmissionEmailBody"]} by {staff.Name} for the week commencing {timesheet.StartDate.ToString("dd/MM/yy")}.</p>");
body.Append($"<p>{Configuration["Email:TimesheetSubmissionEmailEndBody"]}</p>");
body.Append($"<p><a href=\"{Configuration["Authentication:HostUrl"]}/timesheets/addtimesheet/{timesheet.TimesheetId.ToString()}\">Review this timesheet on CapX</a></p>");
body.Append("<p><em>Sent from CapX</em></p>");

// Send email
Debug.WriteLine($"** Sending Timesheet Submission email to {string.Join("|", recipients)}");
SendEmail(recipients, subject, body.ToString());
}
}
}
Expand Down Expand Up @@ -310,9 +312,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);
}
}
}
}
Expand Down Expand Up @@ -470,8 +476,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);
}
}
}
}
Expand Down
Loading