forked from lukencode/FluentEmail
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added easier setup and use of embedded templates (#16)
* Added simplified configuration for embedded templates
- Loading branch information
1 parent
7fdd5cd
commit f1e494c
Showing
10 changed files
with
188 additions
and
9 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
# FluentEmail - All in one email sender for .NET and .NET Core | ||
|
||
The easiest way to send email from .NET and .NET Core. Use Razor for email templates and send using SendGrid, MailGun, SMTP and more. | ||
The easiest way to send email from .NET and .NET Core. Use Razor or Liquid for email templates and send using SendGrid, MailGun, MailKit, SMTP and more. | ||
|
||
Forked from original by **[@lukencode](https://github.com/lukencode/fluentemail)** | ||
|
||
|
@@ -130,6 +130,44 @@ var email = Email | |
.UsingTemplate(template, new ViewModel { Name = "Luke", Compliment = "Awesome" }); | ||
``` | ||
|
||
## Embedded Templates | ||
|
||
There is a set of extensions in `EmbeddedTemplates` that allows for use of embedded templates without specifying the assembly and the path every time. | ||
|
||
```csharp | ||
EmbeddedTemplates.Configure(Assembly.GetExecutingAssembly(), "FluentEmail.Core.Tests"); | ||
var email = Email | ||
.From(fromEmail) | ||
.To(toEmail) | ||
.Subject(subject) | ||
.UsingTemplateFromEmbedded("templatename.liquid", new ViewModel { Name = "Luke", Compliment = "Awesome" }); | ||
``` | ||
|
||
## Embedded Templates with Liquid Renderer | ||
|
||
Because the Liquid templates can also be configured with an embedded provider, there are builder extensions that will configure both the embedded file provider for layouts and the `EmbeddedTemplates` extensions. | ||
|
||
There is a default of the executing assembly with Templates in `EmailTemplates` | ||
|
||
```csharp | ||
builder.Services.AddFluentEmail("[email protected]") | ||
.AddLiquidRendererWithEmbedded(Assembly.GetExecutingAssembly(), "AssemblyName.EmailTemplates") | ||
|
||
// These are the same | ||
builder.Services.AddFluentEmail("[email protected]") | ||
.AddLiquidRendererWithEmbedded() | ||
``` | ||
|
||
## How to set all templates as embedded in the `csproj` file | ||
|
||
If you want all templates in a folder to automatically be embedded, use the following in your `csproj` file. | ||
|
||
```xml | ||
<ItemGroup> | ||
<EmbeddedResource Include="EmailTemplates/**/*.liquid" /> | ||
</ItemGroup> | ||
``` | ||
|
||
## Sending Emails | ||
|
||
```csharp | ||
|
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,60 @@ | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace FluentEmail.Core; | ||
|
||
public static class EmbeddedTemplates | ||
{ | ||
private static Assembly _assembly; | ||
private static string _rootPath; | ||
|
||
public static void Configure(Assembly assembly, string rootPath) | ||
{ | ||
_assembly = assembly; | ||
_rootPath = rootPath; | ||
} | ||
|
||
/// <summary> | ||
/// Adds template to email from previously configured default embedded resource | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="path">Path the the embedded resource eg [YourResourceFolder].[YourFilename.txt]. Will be appended to configured root path</param> | ||
/// <param name="model">Model for the template</param> | ||
/// <param name="isHtml">True if Body is HTML (default), false for plain text</param> | ||
/// <returns></returns> | ||
public static IFluentEmail UsingTemplateFromEmbedded<T>(this IFluentEmail email, string path, T model, bool isHtml = true) | ||
{ | ||
if (_assembly is null) | ||
{ | ||
throw new Exception("FluentEmailEmbeddedExtensions.Configure must be called with default assembly and root path"); | ||
} | ||
|
||
var root = _rootPath; | ||
if (!string.IsNullOrEmpty(root)) root += "."; | ||
var template = EmbeddedResourceHelper.GetResourceAsString(_assembly, $"{root}{path}"); | ||
var result = email.Renderer.Parse(template, model, isHtml); | ||
email.Data.IsHtml = isHtml; | ||
email.Data.Body = result; | ||
|
||
return email; | ||
} | ||
|
||
/// <summary> | ||
/// Adds alternative plaintext template to email from previously configured embedded resource | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="path">Path the the embedded resource eg [YourResourceFolder].[YourFilename.txt]. Will be appended to configured root path</param> | ||
/// <param name="model">Model for the template</param> | ||
/// <returns></returns> | ||
public static IFluentEmail PlaintextAlternativeUsingTemplateFromEmbedded<T>(this IFluentEmail email, string path, T model) | ||
{ | ||
var root = _rootPath; | ||
if (!string.IsNullOrEmpty(root)) root += "."; | ||
var template = EmbeddedResourceHelper.GetResourceAsString(_assembly, $"{root}{path}"); | ||
var result = email.Renderer.Parse(template, model, false); | ||
email.Data.PlaintextAlternativeBody = result; | ||
|
||
return email; | ||
} | ||
|
||
} |
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 @@ | ||
yo email ##Test## |
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