Skip to content

Commit

Permalink
Merge pull request #87 from Lombiq/issue/NEST-423
Browse files Browse the repository at this point in the history
NEST-423: Send separate email for each user
  • Loading branch information
DemeSzabolcs authored Sep 26, 2023
2 parents 0980c54 + f9d1852 commit 44b80b9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static void SetEmailQuotaManagementOptionsForUITest(
{
argumentsBuilder
.AddWithValue(
"OrchardCore:Lombiq_Hosting_Tenants_EmailQuotaManagement:EmailQuota",
"OrchardCore:Lombiq_Hosting_Tenants_EmailQuotaManagement:EmailQuotaPerMonth",
maximumEmails);

argumentsBuilder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.AspNetCore.Identity;
using OrchardCore.Email;
using OrchardCore.Security;
using OrchardCore.Security.Services;
using OrchardCore.Users;
Expand All @@ -25,7 +24,7 @@ public EmailQuotaEmailService(
_userManager = userManager;
}

public async Task<MailMessage> CreateEmailForExceedingQuotaAsync()
public async Task<IEnumerable<string>> CollectUserEmailsForExceedingQuotaAsync()
{
// Get users with site owner permission.
var roles = await _roleService.GetRolesAsync();
Expand All @@ -39,14 +38,6 @@ public async Task<MailMessage> CreateEmailForExceedingQuotaAsync()
siteOwners.AddRange(await _userManager.GetUsersInRoleAsync(role.RoleName));
}

var siteOwnerEmails = siteOwners.Select(user => (user as User)?.Email);
var emailMessage = new MailMessage
{
Bcc = siteOwnerEmails.Join(","),
Subject = "[Action Required] Your DotNest site has run over its e-mail quota",
IsHtmlBody = true,
};

return emailMessage;
return siteOwners.Select(user => (user as User)?.Email);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,32 @@ public async Task<SmtpResult> SendAsync(MailMessage message)
return emailResult;
}

private Task SendAlertEmailIfNecessaryAsync(EmailQuota emailQuota)
private async Task SendAlertEmailIfNecessaryAsync(EmailQuota emailQuota)
{
if (IsSameMonth(_clock.UtcNow, emailQuota.LastReminder)) return Task.CompletedTask;
if (IsSameMonth(_clock.UtcNow, emailQuota.LastReminder)) return;

emailQuota.LastReminder = _clock.UtcNow;
_quotaService.SaveQuota(emailQuota);

// Using deferred task to send the email after the current transaction is committed.
ShellScope.AddDeferredTask(async _ =>
var siteOwnerEmails = await _emailQuotaEmailService.CollectUserEmailsForExceedingQuotaAsync();
var emailMessage = new MailMessage
{
var emailParameters = await _emailQuotaEmailService.CreateEmailForExceedingQuotaAsync();
emailParameters.Body = await _emailTemplateService.RenderEmailTemplateAsync("EmailQuota", new
Subject = T["[Action Required] Your DotNest site has run over its e-mail quota"],
IsHtmlBody = true,
};

foreach (var siteOwnerEmail in siteOwnerEmails)
{
ShellScope.AddDeferredTask(async _ =>
{
HostName = _shellSettings.Name,
emailMessage.To = siteOwnerEmail;
emailMessage.Body = await _emailTemplateService.RenderEmailTemplateAsync("EmailQuota", new
{
HostName = _shellSettings.Name,
});
await _smtpService.SendAsync(emailMessage);
});

await _smtpService.SendAsync(emailParameters);
});

return Task.CompletedTask;
}
}

private static bool IsSameMonth(DateTime date1, DateTime date2) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OrchardCore.Email;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Lombiq.Hosting.Tenants.EmailQuotaManagement.Services;
Expand All @@ -12,5 +13,5 @@ public interface IEmailQuotaEmailService
/// <summary>
/// Creates the <see cref="MailMessage"/> that could be sent to the site owners when the email quota is exceeded.
/// </summary>
Task<MailMessage> CreateEmailForExceedingQuotaAsync();
Task<IEnumerable<string>> CollectUserEmailsForExceedingQuotaAsync();
}

0 comments on commit 44b80b9

Please sign in to comment.