Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 763f094

Browse files
Dynamic content in emails
1 parent acd5d2a commit 763f094

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

‎Webgentle.BookStore/Webgentle.BookStore/Controllers/HomeController.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@ public HomeController(IOptionsSnapshot<NewBookAlertConfig> newBookAlertconfigura
3434
public async Task<ViewResult> Index()
3535
{
3636

37-
UserEmailOptions options = new UserEmailOptions
38-
{
39-
ToEmails = new List<string>() { "test@gmail.com"}
40-
};
37+
//UserEmailOptions options = new UserEmailOptions
38+
//{
39+
// ToEmails = new List<string>() { "test@gmail.com"},
40+
// PlaceHolders = new List<KeyValuePair<string, string>>()
41+
// {
42+
// new KeyValuePair<string, string>("{{UserName}}", "John")
43+
// }
44+
//};
4145

42-
await _emailService.SendTestEmail(options);
46+
//await _emailService.SendTestEmail(options);
4347

4448
//var userId = _userService.GetUserId();
4549
//var isLoggedIn = _userService.IsAuthenticated();

‎Webgentle.BookStore/Webgentle.BookStore/EmailTemplate/TestEmail.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<title></title>
66
</head>
77
<body>
8-
<p>Hello from book store web app</p>
8+
<p>Hello {{UserName}}, <br />
9+
This email is coming from book store web app</p>
910
</body>
1011
</html>

‎Webgentle.BookStore/Webgentle.BookStore/Models/UserEmailOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ public class UserEmailOptions
1010
public List<string> ToEmails { get; set; }
1111
public string Subject { get; set; }
1212
public string Body { get; set; }
13+
public List<KeyValuePair<string, string>> PlaceHolders { get; set; }
1314
}
1415
}

‎Webgentle.BookStore/Webgentle.BookStore/Service/EmailService.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ public class EmailService : IEmailService
1818

1919
public async Task SendTestEmail(UserEmailOptions userEmailOptions)
2020
{
21-
userEmailOptions.Subject = "This is test email subject from book store web app";
22-
userEmailOptions.Body = GetEmailBody("TestEmail");
21+
userEmailOptions.Subject = UpdatePlaceHolders("Hello {{UserName}}, This is test email subject from book store web app", userEmailOptions.PlaceHolders);
22+
23+
userEmailOptions.Body = UpdatePlaceHolders(GetEmailBody("TestEmail"), userEmailOptions.PlaceHolders);
2324

2425
await SendEmail(userEmailOptions);
2526
}
@@ -65,5 +66,21 @@ private string GetEmailBody(string templateName)
6566
var body = File.ReadAllText(string.Format(templatePath, templateName));
6667
return body;
6768
}
69+
70+
private string UpdatePlaceHolders(string text, List<KeyValuePair<string, string>> keyValuePairs)
71+
{
72+
if (!string.IsNullOrEmpty(text) && keyValuePairs != null)
73+
{
74+
foreach (var placeholder in keyValuePairs)
75+
{
76+
if (text.Contains(placeholder.Key))
77+
{
78+
text = text.Replace(placeholder.Key, placeholder.Value);
79+
}
80+
}
81+
}
82+
83+
return text;
84+
}
6885
}
6986
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /