Skip to content

Commit c502655

Browse files
EmailManager: refactor for easier customization in forks
1 parent e8e3f21 commit c502655

File tree

5 files changed

+97
-59
lines changed

5 files changed

+97
-59
lines changed

UACloudLibraryServer/Areas/Identity/Pages/Account/ExternalLogin.cshtml.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,9 @@ public async Task<IActionResult> OnPostConfirmationAsync(string returnUrl = null
172172
values: new { area = "Identity", userId = userId, code = code },
173173
protocol: Request.Scheme);
174174

175-
await EmailManager.Send(
175+
await EmailManager.SendConfirmExternalEmail(
176176
_emailSender,
177177
Input.Email,
178-
"UA Cloud Library - Confirm Your Email",
179-
"Please confirm your email to complete registration.",
180178
callbackUrl
181179
).ConfigureAwait(false);
182180

UACloudLibraryServer/Areas/Identity/Pages/Account/ForgotPassword.cshtml.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,9 @@ public async Task<IActionResult> OnPostAsync()
7272
protocol: Request.Scheme);
7373

7474
//notify user of password reset w/ reset link
75-
StringBuilder sbBody = new StringBuilder();
76-
sbBody.AppendLine("<h1>Reset Password</h1>");
77-
sbBody.AppendLine("<p>A request has been made to reset your password in the CESMII Cloud Library.");
78-
sbBody.AppendLine($"<b>Please click here to <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>reset your password</a>.</b></p>");
79-
sbBody.AppendLine("<p>If you did not make this request, please contact the <a href='mailto:[email protected]'>CESMII DevOps Team</a>.</p>");
80-
sbBody.AppendLine("<p>The CESMII UA Cloud Library is hosted by <a href='https://www.cesmii.org/'>CESMII</a>, the Clean Energy Smart Manufacturing Institute! This Cloud Library contains curated node sets created by CESMII or its members, as well as node sets from the <a href='https://uacloudlibrary.opcfoundation.org/'>OPC Foundation Cloud Library</a>.</p>");
81-
sbBody.AppendLine("<p>Sincerely,<br />CESMII DevOps Team</p>");
82-
83-
await EmailManager.Send(
75+
await EmailManager.SendPasswordReset(
8476
_emailSender,
8577
Input.Email,
86-
"CESMII | Cloud Library | Reset Password",
87-
sbBody.ToString(),
8878
callbackUrl
8979
).ConfigureAwait(false);
9080

UACloudLibraryServer/Areas/Identity/Pages/Account/Manage/Email.cshtml.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,9 @@ public async Task<IActionResult> OnPostChangeEmailAsync()
123123
values: new { area = "Identity", userId = userId, email = Input.NewEmail, code = code },
124124
protocol: Request.Scheme);
125125

126-
await EmailManager.Send(
126+
await EmailManager.SendConfirmEmailChange(
127127
_emailSender,
128128
Input.NewEmail,
129-
"UA Cloud Library - Confirm New Email",
130-
"Please confirm your new email address.",
131129
callbackUrl
132130
).ConfigureAwait(false);
133131

@@ -163,11 +161,9 @@ public async Task<IActionResult> OnPostSendVerificationEmailAsync()
163161
values: new { area = "Identity", userId = userId, code = code },
164162
protocol: Request.Scheme);
165163

166-
await EmailManager.Send(
164+
await EmailManager.SendReconfirmEmail(
167165
_emailSender,
168166
Input.NewEmail,
169-
"UA Cloud Library - Verify Your Email",
170-
"Please verify your email address.",
171167
callbackUrl
172168
).ConfigureAwait(false);
173169

UACloudLibraryServer/Areas/Identity/Pages/Account/Register.cshtml.cs

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -168,36 +168,11 @@ public async Task<IActionResult> OnPostAsync(string returnUrl = null)
168168
protocol: Request.Scheme);
169169

170170
//notify registering user
171-
StringBuilder sbBody = new StringBuilder();
172-
sbBody.AppendLine("<h1>Welcome to the CESMII UA Cloud Library</h1>");
173-
sbBody.AppendLine("<p>Thank you for creating an account on the CESMII UA Cloud Library. ");
174-
if (_userManager.Options.SignIn.RequireConfirmedAccount)
175-
{
176-
sbBody.AppendLine($"<b>Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.</b></p>");
177-
}
178-
sbBody.AppendLine("<p>The CESMII UA Cloud Library is hosted by <a href='https://www.cesmii.org/'>CESMII</a>, the Clean Energy Smart Manufacturing Institute! This Cloud Library contains curated node sets created by CESMII or its members, as well as node sets from the <a href='https://uacloudlibrary.opcfoundation.org/'>OPC Foundation Cloud Library</a>.</p>");
179-
sbBody.AppendLine("<p>Sincerely,<br />CESMII DevOps Team</p>");
180-
181-
await EmailManager.Send(
171+
await EmailManager.SendConfirmRegistration(
182172
_emailSender,
183173
Input.Email,
184-
"CESMII | Cloud Library | New Account Confirmation",
185-
sbBody.ToString(),
186-
callbackUrl
187-
).ConfigureAwait(false);
188-
189-
//notify CESMII dev ops as well
190-
StringBuilder sbBody2 = new StringBuilder();
191-
sbBody2.AppendLine("<h1>CESMII UA Cloud Library - New Account Sign Up</h1>");
192-
sbBody2.AppendLine($"<p>User <b>'{Input.Email}'</b> created an account on the CESMII UA Cloud Library. ");
193-
sbBody2.AppendLine("<p>The CESMII UA Cloud Library is hosted by <a href='https://www.cesmii.org/'>CESMII</a>, the Clean Energy Smart Manufacturing Institute! This Cloud Library contains curated node sets created by CESMII or its members, as well as node sets from the <a href='https://uacloudlibrary.opcfoundation.org/'>OPC Foundation Cloud Library</a>.</p>");
194-
sbBody2.AppendLine("<p>Sincerely,<br />CESMII DevOps Team</p>");
195-
await EmailManager.Send(
196-
_emailSender,
197-
198-
"CESMII | Cloud Library | New Account Sign Up",
199-
sbBody2.ToString(),
200-
callbackUrl
174+
callbackUrl,
175+
_userManager.Options.SignIn.RequireConfirmedAccount
201176
).ConfigureAwait(false);
202177

203178
if (_userManager.Options.SignIn.RequireConfirmedAccount)

UACloudLibraryServer/EmailManager.cs

Lines changed: 90 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@
2727
* http://opcfoundation.org/License/MIT/1.00/
2828
* ======================================================================*/
2929

30+
using System;
3031
using System.Globalization;
32+
using System.Text;
33+
using System.Text.Encodings.Web;
3134
using System.Threading.Tasks;
35+
using Microsoft.AspNetCore.Identity;
3236
using Microsoft.AspNetCore.Identity.UI.Services;
3337

3438
namespace Opc.Ua.Cloud.Library
@@ -37,33 +41,108 @@ public class EmailManager
3741
{
3842
const string EmailTemplate = @"
3943
<meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"">
40-
<p><img decoding=""async"" class=""alignnone wp-image-1095"" style=""margin: 0px; border: 0px none;"" src=""https://opcfoundation.org/wp-content/uploads/2013/09/OPC_Logo_500x72-300x110.jpg"" alt=""OPC Foundation Logo"" width=""240"" height=""95""></p>
41-
<p><b>The Industrial Interoperability Standard ™</b></p>
42-
<p>&nbsp;</p>
4344
<p><p>{0} Please <a href=""{1}"">click here</a> to continue.</p>
4445
<p>If the above link does not work, please copy and paste the link below into your browser’s address bar and press enter:</p>
45-
<p>{1}</p>
46+
<p>{2}</p>
4647
<p>If you experience difficulty with this site, please reply to this email for help.</p>
4748
</p>
48-
<p><strong>OPC Foundation</strong><br>
49-
16101 North 82nd Street, Suite 3B<br>
50-
Scottsdale, Arizona 85260-1868 US<br>
51-
+1 (480) 483-6644<br>
52-
<p style=""text-align: center""><a href=""mailto:[email protected]?subject=Unsubscribe%20from%20UA%20Cloud%20Library%20Emails&body="">Click here to unsubscribe.</a></p></p>
53-
";
49+
<p>The CESMII UA Cloud Library is hosted by <a href='https://www.cesmii.org/'>CESMII</a>, the Clean Energy Smart Manufacturing Institute!
50+
This Cloud Library contains curated node sets created by CESMII or its members, as well as node sets from the <a href='https://uacloudlibrary.opcfoundation.org/'>OPC Foundation Cloud Library</a>.
51+
</p>
52+
";
5453

55-
public static async Task Send(IEmailSender emailSender, string email, string subject, string action, string url)
54+
//const string EmailTemplate = @"
55+
// <meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"">
56+
// <p><img decoding=""async"" class=""alignnone wp-image-1095"" style=""margin: 0px; border: 0px none;"" src=""https://opcfoundation.org/wp-content/uploads/2013/09/OPC_Logo_500x72-300x110.jpg"" alt=""OPC Foundation Logo"" width=""240"" height=""95""></p>
57+
// <p><b>The Industrial Interoperability Standard ™</b></p>
58+
// <p>&nbsp;</p>
59+
// <p><p>{0} Please <a href=""{1}"">click here</a> to continue.</p>
60+
// <p>If the above link does not work, please copy and paste the link below into your browser’s address bar and press enter:</p>
61+
// <p>{2}</p>
62+
// <p>If you experience difficulty with this site, please reply to this email for help.</p>
63+
// </p>
64+
// <p><strong>OPC Foundation</strong><br>
65+
// 16101 North 82nd Street, Suite 3B<br>
66+
// Scottsdale, Arizona 85260-1868 US<br>
67+
// +1 (480) 483-6644<br>
68+
// <p style=""text-align: center""><a href=""mailto:[email protected]?subject=Unsubscribe%20from%20UA%20Cloud%20Library%20Emails&body="">Click here to unsubscribe.</a></p></p>
69+
// ";
70+
71+
private static async Task Send(IEmailSender emailSender, string email, string subject, string action, string url)
5672
{
5773
var body = string.Format(
5874
CultureInfo.InvariantCulture,
5975
EmailTemplate,
6076
action,
77+
HtmlEncoder.Default.Encode(url),
6178
url);
6279

6380
await emailSender.SendEmailAsync(
6481
email,
6582
subject,
6683
body).ConfigureAwait(false);
6784
}
85+
86+
internal static async Task SendConfirmRegistration(IEmailSender emailSender, string email, string url, bool requireConfirmedAccount)
87+
{
88+
StringBuilder sbBody = new StringBuilder();
89+
sbBody.AppendLine("<h1>Welcome to the CESMII UA Cloud Library</h1>");
90+
sbBody.AppendLine("<p>Thank you for creating an account on the CESMII UA Cloud Library. ");
91+
if (requireConfirmedAccount)
92+
{
93+
sbBody.AppendLine($"<b>Please confirm your account by <a href='{HtmlEncoder.Default.Encode(url)}'>clicking here</a>.</b></p>".ToString());
94+
}
95+
sbBody.AppendLine("<p>The CESMII UA Cloud Library is hosted by <a href='https://www.cesmii.org/'>CESMII</a>, the Clean Energy Smart Manufacturing Institute! This Cloud Library contains curated node sets created by CESMII or its members, as well as node sets from the <a href='https://uacloudlibrary.opcfoundation.org/'>OPC Foundation Cloud Library</a>.</p>");
96+
sbBody.AppendLine("<p>Sincerely,<br />CESMII DevOps Team</p>");
97+
await emailSender.SendEmailAsync(
98+
email,
99+
"CESMII | Cloud Library | New Account Confirmation",
100+
sbBody.ToString()).ConfigureAwait(false);
101+
102+
//notify CESMII dev ops as well
103+
StringBuilder sbBody2 = new StringBuilder();
104+
sbBody2.AppendLine("<h1>CESMII UA Cloud Library - New Account Sign Up</h1>");
105+
sbBody2.AppendLine($"<p>User <b>'{email}'</b> created an account on the CESMII UA Cloud Library.".ToString());
106+
sbBody2.AppendLine("<p>The CESMII UA Cloud Library is hosted by <a href='https://www.cesmii.org/'>CESMII</a>, the Clean Energy Smart Manufacturing Institute! This Cloud Library contains curated node sets created by CESMII or its members, as well as node sets from the <a href='https://uacloudlibrary.opcfoundation.org/'>OPC Foundation Cloud Library</a>.</p>");
107+
sbBody2.AppendLine("<p>Sincerely,<br />CESMII DevOps Team</p>");
108+
await emailSender.SendEmailAsync(
109+
110+
"CESMII | Cloud Library | New Account Sign Up",
111+
sbBody2.ToString()).ConfigureAwait(false);
112+
//return Send(emailSender, email, "UA Cloud Library - Confirm Your Email", "Please confirm your email to complete registration.", url);
113+
}
114+
115+
internal static Task SendConfirmExternalEmail(IEmailSender emailSender, string email, string url)
116+
{
117+
return Send(emailSender, email, "CESMII | Cloud Library | Confirm Your Email", "Please confirm your email to complete registration.", url);
118+
//return Send(emailSender, email, "UA Cloud Library - Confirm Your Email", "Please confirm your email to complete registration.", url);
119+
}
120+
121+
internal static Task SendConfirmEmailChange(IEmailSender emailSender, string newEmail, string url)
122+
{
123+
return Send(emailSender, newEmail, "CESMII | Cloud Library | Confirm New Email", "Please confirm your email to complete registration.", url);
124+
}
125+
126+
internal static Task SendPasswordReset(IEmailSender emailSender, string email, string url)
127+
{
128+
StringBuilder sbBody = new StringBuilder();
129+
sbBody.AppendLine("<h1>Reset Password</h1>");
130+
sbBody.AppendLine("<p>A request has been made to reset your password in the CESMII Cloud Library.");
131+
sbBody.AppendLine($"<b>Please click here to <a href='{HtmlEncoder.Default.Encode(url)}'>reset your password</a>.</b></p>".ToString());
132+
sbBody.AppendLine("<p>If you did not make this request, please contact the <a href='mailto:[email protected]'>CESMII DevOps Team</a>.</p>");
133+
sbBody.AppendLine("<p>The CESMII UA Cloud Library is hosted by <a href='https://www.cesmii.org/'>CESMII</a>, the Clean Energy Smart Manufacturing Institute! This Cloud Library contains curated node sets created by CESMII or its members, as well as node sets from the <a href='https://uacloudlibrary.opcfoundation.org/'>OPC Foundation Cloud Library</a>.</p>");
134+
sbBody.AppendLine("<p>Sincerely,<br />CESMII DevOps Team</p>");
135+
return emailSender.SendEmailAsync(
136+
email,
137+
"CESMII | Cloud Library | Reset Password",
138+
sbBody.ToString());
139+
//return Send(emailSender, email, "UA Cloud Library - Reset Password", "We received a request to reset your password.", url);
140+
}
141+
142+
internal static Task SendReconfirmEmail(IEmailSender emailSender, string newEmail, string url)
143+
{
144+
return Send(emailSender, newEmail, "CESMII | Cloud Library | Verify Your Email", "Please verify your email address.", url);
145+
//return Send(emailSender, newEmail, "UA Cloud Library - Verify Your Email","Please verify your email address.", url);
146+
}
68147
}
69148
}

0 commit comments

Comments
 (0)