Skip to content

Commit

Permalink
fix: handle @ in display name when using MailHelper.StringToEmailAddr…
Browse files Browse the repository at this point in the history
…ess (#903)

* Update MailHelper.cs

fix email regex to allow email addresses in the display name.

Input: [email protected] <[email protected]>
Expected:
-NameGroup: [email protected]
-EmailGroup: [email protected]

Initial:
-NameGroup: {empty string}
-EmailGroup: 	[email protected] <[email protected]
Result: A match is found, therefore there is no error caught and email string is bad

After:
-NameGroup: [email protected]
-EmailGroup: [email protected]
Result: Success!

Other test cases:
-<[email protected]> SUCCESS
[email protected] SUCCESS
-username <[email protected]> SUCCESS
-username <usernameexample.com> NO MATCH (SUCCESS)
  • Loading branch information
Fieora authored Apr 9, 2020
1 parent 278735c commit 57ba28b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/SendGrid/Helpers/Mail/MailHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class MailHelper
private const string NameGroup = "name";
private const string EmailGroup = "email";
private static readonly Regex Rfc2822Regex = new Regex(
$@"(?:(?<{NameGroup}>)(?<{EmailGroup}>[^\<]*@.*[^\>])|(?<{NameGroup}>[^\<]*)\<(?<{EmailGroup}>.*@.*)\>)",
$@"(?:(?<{NameGroup}>[^\<]*)\<(?<{EmailGroup}>.*@.*)\>|(?<{NameGroup}>)(?<{EmailGroup}>[^\<]*@.*[^\>]))",
RegexOptions.ECMAScript);

/// <summary>
Expand Down
12 changes: 12 additions & 0 deletions tests/SendGrid.Tests/Integration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6126,6 +6126,18 @@ public async Task TestRetryBehaviourSucceedsOnSecondAttempt()
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
}

[Theory]
[InlineData("first last <[email protected]>", "first last", "[email protected]")]
[InlineData("<[email protected]>", "", "[email protected]")]
[InlineData("[email protected]", "", "[email protected]")]
[InlineData("[email protected] <[email protected]>", "[email protected]", "[email protected]")]
public void TestStringToEmailAddress(string input, string expectedName, string expectedEmailAddress)
{
var actual = MailHelper.StringToEmailAddress(input);

Assert.True(actual.Name == expectedName && actual.Email == expectedEmailAddress);
}

/// <summary>
/// Tests the conditions in issue #670.
/// </summary>
Expand Down

0 comments on commit 57ba28b

Please sign in to comment.