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(ToAzureEmailAddress)];
}

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

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

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(ToAzureEmailAddress(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 ToAzureEmailAddress(string emailWithDisplayName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method name should be a verb, why didn't you add this code to ParseEmailAddressWithDisplayName?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method name should be a verb

Yeah, I initially intended it to be an extension method, I'll fix that.

why didn't you add this code to ParseEmailAddressWithDisplayName?

ParseEmailAddressWithDisplayName is used separately too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more thing can we create a virtual method so subclasses can implement how to get the display name

Copy link
Member Author

@BenedekFarkas BenedekFarkas Oct 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic here is laser-focused on this scenario, so I don't see any reason to override it without overriding the whole email sending logic. But then you can handle email addresses however you want.

{
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();
}