Skip to content

Commit dd80d22

Browse files
committed
Extending mockup feature, update templating version
1 parent 78e307d commit dd80d22

File tree

10 files changed

+135
-32
lines changed

10 files changed

+135
-32
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[![NuGet Pre Release](https://img.shields.io/nuget/vpre/GeekLearning.Email.svg?maxAge=2592000)]()
2+
[![Build Status](https://geeklearning.visualstudio.com/_apis/public/build/definitions/f841b266-7595-4d01-9ee1-4864cf65aa73/28/badge)](#)
3+
14
# gl-dotnet-email
25

36
Coming Soon !

samples/GeekLearning.Email.Samples/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void ConfigureServices(IServiceCollection services)
3434

3535
services.AddStorage().AddFileSystemStorage();
3636
services.Configure<StorageOptions>(Configuration.GetSection("Storage"));
37-
services.AddTemplating().AddMustache();
37+
services.AddTemplating().AddHandlebars();
3838

3939
services.AddEmail();
4040
services.Configure<EmailOptions>(Configuration.GetSection("Email"));

samples/GeekLearning.Email.Samples/appsettings.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@
1414
"Email": "no-reply@geeklearning.io",
1515
"DisplayName": "Geek Learning"
1616
},
17-
"TemplateStorage": "Templates"
17+
"TemplateStorage": "Templates",
18+
"Mockup": {
19+
"Recipients": [ ],
20+
"Exceptions": {
21+
"Emails": [ ],
22+
"Domains": [ ]
23+
}
24+
}
1825
},
1926
"Storage": {
2027
"Stores": {

samples/GeekLearning.Email.Samples/project.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-final",
2121
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc2-final",
2222
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0-rc2-final",
23-
"GeekLearning.Storage.FileSystem": "0.2.0-beta0001",
23+
"GeekLearning.Storage.FileSystem": "0.2.0-beta0002",
24+
"GeekLearning.Templating.Handlebars": "0.2.1-beta0004",
2425
"GeekLearning.Email": "*"
2526
},
2627

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
11
namespace GeekLearning.Email
22
{
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
36
public class EmailOptions
47
{
58
public string Key { get; set; }
69

710
public string User { get; set; }
811

9-
public Implementation.EmailAddress DefaultSender { get; set; }
12+
public Internal.EmailAddress DefaultSender { get; set; }
1013

1114
public string TemplateStorage { get; set; }
1215

13-
public string MockupRecipient { get; set; }
16+
public MockupOptions Mockup { get; set; } = new MockupOptions();
17+
18+
public bool IsMockedUp
19+
{
20+
get
21+
{
22+
return this.Mockup.Recipients.Any()
23+
&& !string.IsNullOrEmpty(this.Mockup.Recipients.First());
24+
}
25+
}
26+
27+
public class MockupOptions
28+
{
29+
public List<string> Recipients { get; set; } = new List<string>();
30+
31+
public MockupExceptionsOptions Exceptions { get; set; } = new MockupExceptionsOptions();
32+
33+
public string Disclaimer { get; set; } = "This email was originally destined to the following recipents, and was mocked up because it was sent from a test environment.";
34+
}
35+
36+
public class MockupExceptionsOptions
37+
{
38+
public List<string> Emails { get; set; } = new List<string>();
39+
40+
public List<string> Domains { get; set; } = new List<string>();
41+
}
1442
}
1543
}

src/GeekLearning.Email/GeekLearningEmailExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public static class GeekLearningEmailExtensions
77
{
88
public static IServiceCollection AddEmail(this IServiceCollection services)
99
{
10-
services.AddSingleton<IEmailSender, Implementation.EmailSender>();
10+
services.AddSingleton<IEmailSender, Internal.EmailSender>();
1111

1212
return services;
1313
}

src/GeekLearning.Email/Implementation/EmailAddress.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace GeekLearning.Email.Internal
2+
{
3+
public class EmailAddress : IEmailAddress
4+
{
5+
public EmailAddress()
6+
{
7+
}
8+
9+
public EmailAddress(string email, string displayName)
10+
{
11+
this.Email = email;
12+
this.DisplayName = displayName;
13+
}
14+
15+
public string Email { get; set; }
16+
17+
public string DisplayName { get; set; }
18+
}
19+
}

src/GeekLearning.Email/Implementation/EmailSender.cs renamed to src/GeekLearning.Email/Internal/EmailSender.cs

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace GeekLearning.Email.Implementation
1+
namespace GeekLearning.Email.Internal
22
{
33
using Microsoft.Extensions.Options;
44
using Newtonsoft.Json;
@@ -68,31 +68,78 @@ private async Task SendGridSend(
6868
string text,
6969
string html)
7070
{
71+
var finalRecipients = new List<IEmailAddress>();
72+
var mockedUpRecipients = new List<IEmailAddress>();
73+
if (this.options.IsMockedUp)
74+
{
75+
foreach (var recipient in recipients)
76+
{
77+
var emailParts = recipient.Email.Split('@');
78+
if (emailParts.Length != 2)
79+
{
80+
throw new NotSupportedException("Bad recipient email.");
81+
}
82+
83+
var domain = emailParts[1];
84+
85+
if (!this.options.Mockup.Exceptions.Emails.Contains(recipient.Email)
86+
&& !this.options.Mockup.Exceptions.Domains.Contains(domain))
87+
{
88+
if (!mockedUpRecipients.Any())
89+
{
90+
foreach (var mockupRecipient in this.options.Mockup.Recipients)
91+
{
92+
finalRecipients.Add(new EmailAddress(mockupRecipient, "Mockup Recipient"));
93+
}
94+
}
95+
96+
mockedUpRecipients.Add(recipient);
97+
}
98+
else
99+
{
100+
finalRecipients.Add(recipient);
101+
}
102+
}
103+
}
104+
else
105+
{
106+
finalRecipients = recipients.ToList();
107+
}
108+
109+
if (mockedUpRecipients.Any())
110+
{
111+
var disclaimer = this.options.Mockup.Disclaimer;
112+
var joinedMockedUpRecipients = string.Join(", ", mockedUpRecipients.Select(r => $"{r.DisplayName} ({r.Email})"));
113+
114+
text = string.Concat(text, Environment.NewLine, disclaimer, Environment.NewLine, joinedMockedUpRecipients);
115+
html = string.Concat(html, "<br/><i>", disclaimer, "<br/>", joinedMockedUpRecipients, "</i>");
116+
}
117+
71118
var client = new HttpClient();
72119
var message = new HttpRequestMessage(HttpMethod.Post, "https://api.sendgrid.com/api/mail.send.json");
73120

74-
var variables = new Dictionary<string, string>()
121+
var variables = new List<KeyValuePair<string, string>>()
75122
{
76-
["api_user"] = apiUser,
77-
["api_key"] = apiKey,
78-
["from"] = from.Email,
79-
["fromname"] = from.DisplayName,
80-
["subject"] = subject,
81-
["text"] = text,
82-
["html"] = html,
123+
new KeyValuePair<string, string>("api_user", apiUser),
124+
new KeyValuePair<string, string>("api_key", apiKey),
125+
new KeyValuePair<string, string>("from", from.Email),
126+
new KeyValuePair<string, string>("fromname", from.DisplayName),
127+
new KeyValuePair<string, string>("subject", subject),
128+
new KeyValuePair<string, string>("text", text),
129+
new KeyValuePair<string, string>("html", html),
83130
};
84131

85-
if (recipients.Count() == 1)
132+
if (finalRecipients.Count() == 1)
86133
{
87-
variables["to"] = string.IsNullOrEmpty(this.options.MockupRecipient) ? recipients.First().Email : this.options.MockupRecipient;
88-
variables["toname"] = recipients.First().DisplayName;
134+
variables.Add(new KeyValuePair<string, string>("to", recipients.First().Email));
135+
variables.Add(new KeyValuePair<string, string>("toname", recipients.First().DisplayName));
89136
}
90137
else
91138
{
92-
foreach (var recipient in recipients)
139+
foreach (var recipient in finalRecipients)
93140
{
94-
variables["to[]"] = string.IsNullOrEmpty(this.options.MockupRecipient) ? recipient.Email : this.options.MockupRecipient;
95-
variables["toname[]"] = recipient.DisplayName;
141+
variables.Add(new KeyValuePair<string, string>("to[]", recipient.Email));
142+
variables.Add(new KeyValuePair<string, string>("toname[]", recipient.DisplayName));
96143
}
97144
}
98145

src/GeekLearning.Email/project.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
{
22
"version": "1.0.0-*",
3+
"description": "Templated Emails Library",
4+
"authors": [ "Geek Learning", "Adrien Siffermann", "Cyprien Autexier" ],
5+
"packOptions": {
6+
"tags": [ ],
7+
"projectUrl": "",
8+
"licenseUrl": ""
9+
},
310

411
"dependencies": {
5-
"GeekLearning.Templating": "0.2.1-beta0002",
612
"NETStandard.Library": "1.5.0-rc2-24027",
713
"Microsoft.Extensions.Options": "1.0.0-rc2-final",
814
"Microsoft.Extensions.Caching.Abstractions": "1.0.0-rc2-final",
915
"Newtonsoft.Json": "8.0.3",
10-
"System.Net.Http": "4.0.1-rc2-24027"
16+
"GeekLearning.Templating": "0.2.1-beta0004"
1117
},
1218

1319
"frameworks": {
20+
"net451": { },
1421
"netstandard1.5": {
1522
"imports": [
1623
"dotnet5.6",

0 commit comments

Comments
 (0)