Skip to content

Commit

Permalink
fix issues with email sending
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronontheweb committed May 6, 2024
1 parent bf79362 commit 349bd3d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
34 changes: 31 additions & 3 deletions src/DrawTogether.Email/MailGunEmailSender.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,13 +9,13 @@ namespace DrawTogether.Email;
/// <summary>
/// Wraps FluentEmail to send emails using MailGun within the context of ASP.NET Core Identity.
/// </summary>
public sealed class MailGunEmailSender : IEmailSender
public sealed class MailGunEmailSender<TUser> : IEmailSender<TUser>, IEmailSender where TUser : class
{
private readonly ILogger<MailGunEmailSender> _logger;
private readonly ILogger<MailGunEmailSender<TUser>> _logger;
private readonly ISender _mailgunSender;
private readonly EmailSettings _emailSettings;

public MailGunEmailSender(ISender mailgunSender, IOptions<EmailSettings> emailSettings, ILogger<MailGunEmailSender> logger)
public MailGunEmailSender(ISender mailgunSender, IOptions<EmailSettings> emailSettings, ILogger<MailGunEmailSender<TUser>> logger)
{
_logger = logger;
_mailgunSender = mailgunSender;
Expand All @@ -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: <a href='{confirmationLink}'>Confirm Email</a>";

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: <a href='{resetLink}'>Reset Password</a>";

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);
}
}
15 changes: 12 additions & 3 deletions src/DrawTogether.Email/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -6,23 +7,31 @@ namespace DrawTogether.Email;

public static class ServiceCollectionExtensions
{
public static IServiceCollection AddEmailServices(this IServiceCollection services, IConfiguration configuration)
public static IServiceCollection AddEmailServices<TUser>(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<EmailSettings>(configuration.GetSection("EmailSettings"));

services.AddFluentEmail(configuration["EmailSettings:FromAddress"], configuration["EmailSettings:FromName"])
.AddRazorRenderer()
.AddMailGunSender(configuration["EmailSettings:MailgunDomain"],
configuration["EmailSettings:MailgunApiKey"]);

services.AddTransient<IEmailSender, MailGunEmailSender>();
services.AddTransient<IEmailSender, MailGunEmailSender<TUser>>();
services.AddTransient<IEmailSender<TUser>, MailGunEmailSender<TUser>>();

return services;
}
Expand Down
10 changes: 8 additions & 2 deletions src/DrawTogether/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -16,7 +18,11 @@
builder.Services.AddScoped<IdentityUserAccessor>();
builder.Services.AddScoped<IdentityRedirectManager>();
builder.Services.AddScoped<AuthenticationStateProvider, IdentityRevalidatingAuthenticationStateProvider>();
builder.Services.AddEmailServices(builder.Configuration); // add email services
builder.Services.AddEmailServices<ApplicationUser>(builder.Configuration); // add email services

// if an email provider is not registered, add the no op email provider
builder.Services.TryAddSingleton<IEmailSender, NoOpEmailSender>();
builder.Services.TryAddSingleton<IEmailSender<ApplicationUser>, IdentityNoOpEmailSender>();

builder.Services.AddAuthentication(options =>
{
Expand All @@ -35,7 +41,7 @@
.AddSignInManager()
.AddDefaultTokenProviders();

builder.Services.AddSingleton<IEmailSender<ApplicationUser>, IdentityNoOpEmailSender>();
builder.Services.Configure<RouteOptions>(options => options.LowercaseUrls = true);

var app = builder.Build();

Expand Down
6 changes: 5 additions & 1 deletion src/DrawTogether/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"EmailSettings": {
"FromAddress": "[email protected]",
"FromName": "DrawTogether.NET"
}
}

0 comments on commit 349bd3d

Please sign in to comment.