From 349bd3d87a9836d35be6eb5973590204cc7f5ca8 Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Mon, 6 May 2024 09:59:42 +0200 Subject: [PATCH] fix issues with email sending --- src/DrawTogether.Email/MailGunEmailSender.cs | 34 +++++++++++++++++-- .../ServiceCollectionExtensions.cs | 15 ++++++-- src/DrawTogether/Program.cs | 10 ++++-- src/DrawTogether/appsettings.json | 6 +++- 4 files changed, 56 insertions(+), 9 deletions(-) diff --git a/src/DrawTogether.Email/MailGunEmailSender.cs b/src/DrawTogether.Email/MailGunEmailSender.cs index 7da6a13..c4b4102 100644 --- a/src/DrawTogether.Email/MailGunEmailSender.cs +++ b/src/DrawTogether.Email/MailGunEmailSender.cs @@ -1,4 +1,5 @@ using FluentEmail.Core.Interfaces; +using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.UI.Services; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -8,13 +9,13 @@ namespace DrawTogether.Email; /// /// Wraps FluentEmail to send emails using MailGun within the context of ASP.NET Core Identity. /// -public sealed class MailGunEmailSender : IEmailSender +public sealed class MailGunEmailSender : IEmailSender, IEmailSender where TUser : class { - private readonly ILogger _logger; + private readonly ILogger> _logger; private readonly ISender _mailgunSender; private readonly EmailSettings _emailSettings; - public MailGunEmailSender(ISender mailgunSender, IOptions emailSettings, ILogger logger) + public MailGunEmailSender(ISender mailgunSender, IOptions emailSettings, ILogger> logger) { _logger = logger; _mailgunSender = mailgunSender; @@ -33,4 +34,31 @@ public Task SendEmailAsync(string email, string subject, string htmlMessage) return _mailgunSender.SendAsync(mailMessage); } + + public Task SendConfirmationLinkAsync(TUser user, string email, string confirmationLink) + { + // format an email with the confirmation link + var subject = "Confirm your email with DrawTogether"; + var body = $"Please confirm your email by clicking this link: Confirm Email"; + + return SendEmailAsync(email, subject, body); + } + + public Task SendPasswordResetLinkAsync(TUser user, string email, string resetLink) + { + // format an email with the reset link + var subject = "Reset your password with DrawTogether"; + var body = $"Please reset your password by clicking this link: Reset Password"; + + return SendEmailAsync(email, subject, body); + } + + public Task SendPasswordResetCodeAsync(TUser user, string email, string resetCode) + { + // format an email with the reset code + var subject = "Reset your password with DrawTogether"; + var body = $"Please reset your password using this code: {resetCode}"; + + return SendEmailAsync(email, subject, body); + } } \ No newline at end of file diff --git a/src/DrawTogether.Email/ServiceCollectionExtensions.cs b/src/DrawTogether.Email/ServiceCollectionExtensions.cs index d513769..b604666 100644 --- a/src/DrawTogether.Email/ServiceCollectionExtensions.cs +++ b/src/DrawTogether.Email/ServiceCollectionExtensions.cs @@ -1,3 +1,4 @@ +using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.UI.Services; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -6,15 +7,22 @@ namespace DrawTogether.Email; public static class ServiceCollectionExtensions { - public static IServiceCollection AddEmailServices(this IServiceCollection services, IConfiguration configuration) + public static IServiceCollection AddEmailServices(this IServiceCollection services, IConfiguration configuration) where TUser : class { - // check to see if the EmaiLSettings section is configured in the appsettings.json file + // check to see if the EmailSettings section is configured in the appsettings.json file if (!configuration.GetSection("EmailSettings").Exists()) { // bail out early if email is not configured return services; } + // also check to see if either of the Mailgun settings are configured + if (string.IsNullOrWhiteSpace(configuration["EmailSettings:MailgunDomain"]) || + string.IsNullOrWhiteSpace(configuration["EmailSettings:MailgunApiKey"])) + { + return services; + } + services.Configure(configuration.GetSection("EmailSettings")); services.AddFluentEmail(configuration["EmailSettings:FromAddress"], configuration["EmailSettings:FromName"]) @@ -22,7 +30,8 @@ public static IServiceCollection AddEmailServices(this IServiceCollection servic .AddMailGunSender(configuration["EmailSettings:MailgunDomain"], configuration["EmailSettings:MailgunApiKey"]); - services.AddTransient(); + services.AddTransient>(); + services.AddTransient, MailGunEmailSender>(); return services; } diff --git a/src/DrawTogether/Program.cs b/src/DrawTogether/Program.cs index e4014be..f8ca6af 100644 --- a/src/DrawTogether/Program.cs +++ b/src/DrawTogether/Program.cs @@ -5,6 +5,8 @@ using DrawTogether.Components.Account; using DrawTogether.Data; using DrawTogether.Email; +using Microsoft.AspNetCore.Identity.UI.Services; +using Microsoft.Extensions.DependencyInjection.Extensions; var builder = WebApplication.CreateBuilder(args); @@ -16,7 +18,11 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); -builder.Services.AddEmailServices(builder.Configuration); // add email services +builder.Services.AddEmailServices(builder.Configuration); // add email services + +// if an email provider is not registered, add the no op email provider +builder.Services.TryAddSingleton(); +builder.Services.TryAddSingleton, IdentityNoOpEmailSender>(); builder.Services.AddAuthentication(options => { @@ -35,7 +41,7 @@ .AddSignInManager() .AddDefaultTokenProviders(); -builder.Services.AddSingleton, IdentityNoOpEmailSender>(); +builder.Services.Configure(options => options.LowercaseUrls = true); var app = builder.Build(); diff --git a/src/DrawTogether/appsettings.json b/src/DrawTogether/appsettings.json index 9aa7fc4..59b87d3 100644 --- a/src/DrawTogether/appsettings.json +++ b/src/DrawTogether/appsettings.json @@ -8,5 +8,9 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "EmailSettings": { + "FromAddress": "notifications@nugetupdates.com", + "FromName": "DrawTogether.NET" + } }