-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
SNOW-23: Adding email template management and deferred email sending
- Loading branch information
Showing
11 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
Extensions/Emails/Extensions/EmailSenderShellScopeExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Lombiq.HelpfulExtensions.Extensions.Emails.Models; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using OrchardCore.Email; | ||
using OrchardCore.Environment.Shell.Scope; | ||
|
||
namespace Lombiq.HelpfulExtensions.Extensions.Emails.Extensions | ||
{ | ||
public static class EmailSenderShellScopeExtensions | ||
{ | ||
/// <summary> | ||
/// Sends an HTML email after the current shell scope has ended. If any errors occur during the process they | ||
/// will be logged. | ||
/// </summary> | ||
/// <param name="parameters">Parameters required for sending emails (e.g., recipients, subject, CC).</param> | ||
public static void SendEmailDeferred(this ShellScope shellScope, EmailParameters parameters) => | ||
shellScope.AddDeferredTask(async scope => | ||
{ | ||
var smtpService = scope.ServiceProvider.GetService<ISmtpService>(); | ||
var result = await smtpService.SendAsync(new MailMessage | ||
{ | ||
Sender = parameters.Sender, | ||
To = parameters.To?.Join(","), | ||
Cc = parameters.Cc?.Join(","), | ||
Bcc = parameters.Bcc?.Join(","), | ||
Subject = parameters.Subject, | ||
ReplyTo = parameters.ReplyTo, | ||
Body = parameters.Body, | ||
IsBodyHtml = true, | ||
}); | ||
|
||
if (!result.Succeeded) | ||
{ | ||
var logger = scope.ServiceProvider.GetService<ILogger<ShellScope>>(); | ||
logger.LogError("Email sending was unsuccessful: {0}", result.Errors.Select(error => error.ToString()).Join()); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Lombiq.HelpfulExtensions.Extensions.Emails.Models | ||
{ | ||
public class EmailParameters | ||
{ | ||
public string Sender { get; set; } | ||
public IEnumerable<string> To { get; set; } | ||
public IEnumerable<string> Cc { get; set; } | ||
public IEnumerable<string> Bcc { get; set; } | ||
public string Subject { get; set; } | ||
public string Body { get; set; } | ||
public string ReplyTo { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace Lombiq.HelpfulExtensions.Extensions.Emails.Services | ||
{ | ||
/// <summary> | ||
/// Service for managing email templates. | ||
/// </summary> | ||
public interface IEmailTemplateService | ||
{ | ||
/// <summary> | ||
/// Renders an email template content identified by its ID. | ||
/// </summary> | ||
/// <param name="emailTemplateId">ID of the email template.</param> | ||
/// <param name="model">Optional model used as replacements in the email template.</param> | ||
/// <returns>Rendered email template.</returns> | ||
Task<string> RenderEmailTemplateAsync(string emailTemplateId, object model = null); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Extensions/Emails/Services/ShapeBasedEmailTemplateService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Threading.Tasks; | ||
using Lombiq.HelpfulLibraries.Libraries.Shapes; | ||
using Lombiq.HelpfulLibraries.Libraries.Utilities; | ||
using OrchardCore.DisplayManagement; | ||
|
||
namespace Lombiq.HelpfulExtensions.Extensions.Emails.Services | ||
{ | ||
public class ShapeBasedEmailTemplateService : IEmailTemplateService | ||
{ | ||
private readonly IShapeFactory _shapeFactory; | ||
private readonly IShapeRenderer _shapeRenderer; | ||
|
||
public ShapeBasedEmailTemplateService(IShapeFactory shapeFactory, IShapeRenderer shapeRenderer) | ||
{ | ||
_shapeFactory = shapeFactory; | ||
_shapeRenderer = shapeRenderer; | ||
} | ||
|
||
public async Task<string> RenderEmailTemplateAsync(string emailTemplateId, object model = null) | ||
{ | ||
ExceptionHelpers.ThrowIfNull(emailTemplateId, nameof(emailTemplateId)); | ||
|
||
var shape = await _shapeFactory.CreateAsync($"EmailTemplate__{emailTemplateId}", Arguments.From(model ?? new { })); | ||
|
||
return await _shapeRenderer.RenderAsync(shape); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using Lombiq.HelpfulExtensions.Extensions.Emails.Services; | ||
using Lombiq.HelpfulLibraries.Libraries.Shapes; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OrchardCore.Modules; | ||
|
||
namespace Lombiq.HelpfulExtensions.Extensions.Emails | ||
{ | ||
[Feature(FeatureIds.Emails)] | ||
public class Startup : StartupBase | ||
{ | ||
public override void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddShapeRenderer(); | ||
services.AddScoped<IEmailTemplateService, ShapeBasedEmailTemplateService>(); | ||
} | ||
|
||
public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider) | ||
{ | ||
// No need for anything here yet. | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@* Override this shape to add generic injections email templates (e.g., header, footer, etc.). *@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@* Override this shape to add a title to the email templates. *@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
@{ | ||
const string blockName = "emailTemplate"; | ||
} | ||
|
||
<!DOCTYPE html> | ||
<html lang="@Orchard.CultureName()"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width" initial-scale="1"> | ||
<!--[if !mso]> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<![endif]--> | ||
<title> | ||
<shape type="EmailTemplate_Title"></shape> | ||
</title> | ||
|
||
@await RenderSectionAsync("Head", required: false) | ||
|
||
<shape type="EmailTemplate_LayoutInjections" prop-layoutModel="@Model"></shape> | ||
</head> | ||
<body> | ||
|
||
<span class="preheader" style="display: none; max-height: 0px; overflow: hidden;"> | ||
@await RenderSectionAsync("Preheader", required: false) | ||
</span> | ||
|
||
<div class="@(blockName)"> | ||
<div class="@(blockName)__header"> | ||
@await RenderSectionAsync("Header", required: false) | ||
</div> | ||
|
||
<div class="@(blockName)__content"> | ||
@await RenderSectionAsync("BeforeContent", required: false) | ||
@await RenderBodyAsync() | ||
@await RenderSectionAsync("AfterContent", required: false) | ||
</div> | ||
|
||
<div class="@(blockName)__footer"> | ||
@await RenderSectionAsync("Footer", required: false) | ||
</div> | ||
</div> | ||
|
||
|
||
</body> | ||
</html> |