Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email.Azure: Deconstructing email addresses that include a display name (Lombiq Technologies: OCORE-204) #16889

Merged
merged 8 commits into from
Oct 28, 2024
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.RegularExpressions;
using Azure;
using Azure.Communication.Email;
using Microsoft.Extensions.Localization;
Expand Down Expand Up @@ -161,19 +162,19 @@ private EmailMessage FromMailMessage(MailMessage message, Dictionary<string, ILi
List<EmailAddress> toRecipients = null;
if (recipients.To.Count > 0)
{
toRecipients = [.. recipients.To.Select(r => new EmailAddress(r))];
toRecipients = [.. recipients.To.Select(ConvertEmailAddressToAzureEmailAddress)];
}

List<EmailAddress> ccRecipients = null;
if (recipients.Cc.Count > 0)
{
ccRecipients = [.. recipients.Cc.Select(r => new EmailAddress(r))];
ccRecipients = [.. recipients.Cc.Select(ConvertEmailAddressToAzureEmailAddress)];
}

List<EmailAddress> bccRecipients = null;
if (recipients.Bcc.Count > 0)
{
bccRecipients = [.. recipients.Bcc.Select(r => new EmailAddress(r))];
bccRecipients = [.. recipients.Bcc.Select(ConvertEmailAddressToAzureEmailAddress)];
}

var content = new EmailContent(message.Subject);
Expand All @@ -187,13 +188,14 @@ private EmailMessage FromMailMessage(MailMessage message, Dictionary<string, ILi
}

var emailMessage = new EmailMessage(
message.From,
// For compatibility with configuration for other providers that allow a sender with display name.
ParseEmailAddressWithDisplayName(message.From).EmailAddress,
new EmailRecipients(toRecipients, ccRecipients, bccRecipients),
content);

foreach (var address in message.GetReplyTo())
{
emailMessage.ReplyTo.Add(new EmailAddress(address));
emailMessage.ReplyTo.Add(ConvertEmailAddressToAzureEmailAddress(address));
}

foreach (var attachment in message.Attachments)
Expand Down Expand Up @@ -224,4 +226,33 @@ private EmailMessage FromMailMessage(MailMessage message, Dictionary<string, ILi

return emailMessage;
}

private static EmailAddress ConvertEmailAddressToAzureEmailAddress(string emailWithDisplayName)
{
var (displayName, emailAddress) = ParseEmailAddressWithDisplayName(emailWithDisplayName);

return new EmailAddress(emailAddress, displayName);
}

private static (string DisplayName, string EmailAddress) ParseEmailAddressWithDisplayName(string emailWithDisplayName)
{
var match = AzureEmailProviderBaseRegexes.ParseEmailAddressWithDisplayNameRegex().Match(emailWithDisplayName);
BenedekFarkas marked this conversation as resolved.
Show resolved Hide resolved

if (match.Success)
{
var displayName = match.Groups["displayName"].Value.Trim();
var emailAddress = match.Groups["emailAddress"].Value.Trim();

return (displayName, emailAddress);
}

return (string.Empty, emailWithDisplayName);
}
}

// The regex needs to be in a partial class due to the generated source.
public partial class AzureEmailProviderBaseRegexes
{
[GeneratedRegex(@"^(?:(?<displayName>[^<]*)\s)?<(?<emailAddress>[^>]+)>$")]
public static partial Regex ParseEmailAddressWithDisplayNameRegex();
}