diff --git a/function/Service/IConfigurationService.cs b/function/Service/IConfigurationService.cs index 1ed1566..bc4a71a 100644 --- a/function/Service/IConfigurationService.cs +++ b/function/Service/IConfigurationService.cs @@ -19,6 +19,7 @@ public class ConfigurationServiceWellKnownKeys public const string MailjetApiSecret = nameof(MailjetApiSecret); public const string BaseFunctionUrl = nameof(BaseFunctionUrl); public const string MailjetSenderEmail = nameof(MailjetSenderEmail); + public const string AzureAcsConnectionString = nameof(AzureAcsConnectionString); } public class EnvironmentConfigurationService : IConfigurationService diff --git a/function/Service/UserService.cs b/function/Service/UserService.cs index 592da95..dbf55cd 100644 --- a/function/Service/UserService.cs +++ b/function/Service/UserService.cs @@ -10,6 +10,8 @@ using Microsoft.Extensions.Primitives; using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using Azure; +using Azure.Communication.Email; [assembly: InternalsVisibleTo("Surfrider.PlasticOrigins.Backend.Mobile.Tests")] namespace Surfrider.PlasticOrigins.Backend.Mobile.Service @@ -35,6 +37,7 @@ internal class UserService : IUserService private readonly string _mailJetApiKey; private readonly string _mailjetApiSecret; private readonly string _mailjetSenderEmail; + private readonly string _azureAcsConnectionString; private string _functionBaseUrl; // TODO This is a hack. We should have something like a "FilesystemService" @@ -49,6 +52,7 @@ public UserService(IUserStore userStore, IConfigurationService configurationServ _mailjetApiSecret = configurationService.GetValue(ConfigurationServiceWellKnownKeys.MailjetApiSecret); _functionBaseUrl = configurationService.GetValue(ConfigurationServiceWellKnownKeys.BaseFunctionUrl); _mailjetSenderEmail = configurationService.GetValue(ConfigurationServiceWellKnownKeys.MailjetSenderEmail); + _azureAcsConnectionString = configurationService.GetValue(ConfigurationServiceWellKnownKeys.AzureAcsConnectionString); } public async Task Register(string lastName, string firstName, string birthYear, string email, @@ -186,6 +190,13 @@ private string GenerateUserToken(string email, DateTime validityDate, string use private async Task SendEmail(string to, string content, string textContent, string subject) { + // If Azure ACS is configured, use this instead of Mailjet + if(!string.IsNullOrEmpty(_azureAcsConnectionString)) + { + await SendEmailAzureAcs(to, content, textContent, subject); + return; + } + if (string.IsNullOrWhiteSpace(_mailJetApiKey) || string.IsNullOrWhiteSpace(_mailjetApiSecret)) { Console.WriteLine("No email configured, skipping email sending."); @@ -242,6 +253,17 @@ private async Task SendEmail(string to, string content, string textContent, stri throw new ApplicationException("Unable to send email"); } + private async Task SendEmailAzureAcs(string to, string content, string textContent, string subject) + { + EmailClient emailClient = new EmailClient(_azureAcsConnectionString); + EmailSendOperation emailSendOperation = await emailClient.SendAsync( + Azure.WaitUntil.Started, + _mailjetSenderEmail, + to, + subject, + content); + } + internal static string InternalGenerateUserToken(string email, DateTime validityDate, string userId, string signatureKey, bool isValidationEmail) { var payload = new JwtTokenContent() diff --git a/function/Surfrider.PlasticOrigins.Backend.Mobile.csproj b/function/Surfrider.PlasticOrigins.Backend.Mobile.csproj index 618a11e..923d41f 100644 --- a/function/Surfrider.PlasticOrigins.Backend.Mobile.csproj +++ b/function/Surfrider.PlasticOrigins.Backend.Mobile.csproj @@ -16,6 +16,7 @@ +