-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5858720
commit 80470dc
Showing
9 changed files
with
103 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace DrawTogether.Email; | ||
|
||
public class EmailSettings | ||
{ | ||
public required string MailgunDomain { get; set; } | ||
|
||
public required string MailgunApiKey { get; set; } | ||
|
||
public required string FromAddress { get; set; } | ||
|
||
public required string FromName { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using FluentEmail.Core.Interfaces; | ||
using Microsoft.AspNetCore.Identity.UI.Services; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
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 | ||
{ | ||
private readonly ILogger<MailGunEmailSender> _logger; | ||
private readonly ISender _mailgunSender; | ||
private readonly EmailSettings _emailSettings; | ||
|
||
public MailGunEmailSender(ISender mailgunSender, IOptions<EmailSettings> emailSettings, ILogger<MailGunEmailSender> logger) | ||
{ | ||
_logger = logger; | ||
_mailgunSender = mailgunSender; | ||
_emailSettings = emailSettings.Value; | ||
} | ||
|
||
public Task SendEmailAsync(string email, string subject, string htmlMessage) | ||
{ | ||
_logger.LogDebug("Sending email to {Email} with subject {Subject}", email, subject); | ||
|
||
var mailMessage = FluentEmail.Core.Email | ||
.From(_emailSettings.FromAddress, _emailSettings.FromName) | ||
.To(email) | ||
.Subject(subject) | ||
.Body(htmlMessage, true); | ||
|
||
return _mailgunSender.SendAsync(mailMessage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Microsoft.AspNetCore.Identity.UI.Services; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace DrawTogether.Email; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddEmailServices(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
// 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; | ||
} | ||
|
||
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>(); | ||
|
||
return services; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters