Skip to content

Commit

Permalink
feat: Send email through Azure Communication Services
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaneu committed Jan 10, 2024
1 parent 192bb13 commit 07c8459
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions function/Service/IConfigurationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions function/Service/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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<User> Register(string lastName, string firstName, string birthYear, string email,
Expand Down Expand Up @@ -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.");
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions function/Surfrider.PlasticOrigins.Backend.Mobile.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Azure.Communication.Email" Version="1.0.1" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi" Version="0.7.2-preview" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.0" />
</ItemGroup>
Expand Down

0 comments on commit 07c8459

Please sign in to comment.