forked from lukencode/FluentEmail
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathEmbeddedTemplates.cs
More file actions
60 lines (51 loc) · 2.31 KB
/
EmbeddedTemplates.cs
File metadata and controls
60 lines (51 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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("FluentEmail.Core.EmbeddedTemplates.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;
}
}