From 0564d55396da1f811aa7fa624a8f42392aeb49b9 Mon Sep 17 00:00:00 2001 From: whoswhip Date: Tue, 10 Mar 2026 03:20:48 -0400 Subject: [PATCH] feat(email): add email verification html templates --- SharpbinV3.Server/EmailTemplates/Base.html | 108 ++++++++++++++++++ .../EmailTemplates/VerifyEmail.html | 12 ++ SharpbinV3.Server/Services/AuthService.cs | 9 +- SharpbinV3.Server/Services/EmailTemplates.cs | 41 +++++++ SharpbinV3.Server/SharpbinV3.Server.csproj | 4 + 5 files changed, 169 insertions(+), 5 deletions(-) create mode 100644 SharpbinV3.Server/EmailTemplates/Base.html create mode 100644 SharpbinV3.Server/EmailTemplates/VerifyEmail.html create mode 100644 SharpbinV3.Server/Services/EmailTemplates.cs diff --git a/SharpbinV3.Server/EmailTemplates/Base.html b/SharpbinV3.Server/EmailTemplates/Base.html new file mode 100644 index 0000000..b5bd4a8 --- /dev/null +++ b/SharpbinV3.Server/EmailTemplates/Base.html @@ -0,0 +1,108 @@ + + + + + + {{TITLE}} + + + + +
+
+

Sharpbin

+
+
+ {{CONTENT}} +
+ +
+ + diff --git a/SharpbinV3.Server/EmailTemplates/VerifyEmail.html b/SharpbinV3.Server/EmailTemplates/VerifyEmail.html new file mode 100644 index 0000000..0631cb7 --- /dev/null +++ b/SharpbinV3.Server/EmailTemplates/VerifyEmail.html @@ -0,0 +1,12 @@ +

Verify Your Email Address

+

Hi {{USERNAME}},

+

Thank you for signing up for Sharpbin. To complete your registration, please verify your email address by clicking the button below.

+

+ Verify Email Address +

+

If the button doesn't work, you can copy and paste this link into your browser:

+ +

This link will expire in 24 hours.

+

If you didn't create an account with Sharpbin, you can safely ignore this email.

diff --git a/SharpbinV3.Server/Services/AuthService.cs b/SharpbinV3.Server/Services/AuthService.cs index ac5f2b1..5e09088 100644 --- a/SharpbinV3.Server/Services/AuthService.cs +++ b/SharpbinV3.Server/Services/AuthService.cs @@ -230,11 +230,10 @@ namespace SharpbinV3.Server.Services string token = Utilities.GenerateSecureRandomString(EmailVerificationTokenLength); string tokenHash = Utilities.ComputeSha256(token); - await _email.SendAsync( - user.Email, - "Verify Your Email", - $"{(_appSettings.Https ? "https" : "http")}://{_appSettings.Domain}/verify-email?token={token}" - ); + string verificationUrl = $"{(_appSettings.Https ? "https" : "http")}://{_appSettings.Domain}/verify-email?token={token}"; + string emailBody = EmailTemplates.VerifyEmail(verificationUrl, user.Username); + + await _email.SendAsync(user.Email, "Verify Your Email - Sharpbin", emailBody); var verificationToken = new EmailVerificationToken { TokenHash = tokenHash, diff --git a/SharpbinV3.Server/Services/EmailTemplates.cs b/SharpbinV3.Server/Services/EmailTemplates.cs new file mode 100644 index 0000000..eb48f57 --- /dev/null +++ b/SharpbinV3.Server/Services/EmailTemplates.cs @@ -0,0 +1,41 @@ +using System.Reflection; + +namespace SharpbinV3.Server.Services +{ + public static class EmailTemplates + { + private static readonly Assembly Assembly = Assembly.GetExecutingAssembly(); + private static readonly string BaseTemplateName = "SharpbinV3.Server.EmailTemplates.Base.html"; + + private static string LoadEmbeddedResource(string resourceName) + { + using var stream = Assembly.GetManifestResourceStream(resourceName); + if (stream == null) + throw new FileNotFoundException($"Embedded resource not found: {resourceName}"); + + using var reader = new StreamReader(stream); + return reader.ReadToEnd(); + } + + private static string RenderTemplate(string title, string contentTemplate, Dictionary replacements) + { + var baseTemplate = LoadEmbeddedResource(BaseTemplateName); + var content = contentTemplate; + + foreach (var (key, value) in replacements) + { + content = content.Replace($"{{{{{key}}}}}", value); + } + + return baseTemplate.Replace("{{TITLE}}", title).Replace("{{CONTENT}}", content); + } + + public static string VerifyEmail(string verificationUrl, string username) + { + var contentTemplate = LoadEmbeddedResource("SharpbinV3.Server.EmailTemplates.VerifyEmail.html"); + var replacements = new Dictionary { { "USERNAME", username }, { "VERIFICATION_URL", verificationUrl } }; + + return RenderTemplate("Verify Your Email", contentTemplate, replacements); + } + } +} diff --git a/SharpbinV3.Server/SharpbinV3.Server.csproj b/SharpbinV3.Server/SharpbinV3.Server.csproj index 7834403..75f34fe 100644 --- a/SharpbinV3.Server/SharpbinV3.Server.csproj +++ b/SharpbinV3.Server/SharpbinV3.Server.csproj @@ -36,4 +36,8 @@ + + + +