Skip to content

Commit 57ba28b

Browse files
authored
fix: handle @ in display name when using MailHelper.StringToEmailAddress (#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)
1 parent 278735c commit 57ba28b

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/SendGrid/Helpers/Mail/MailHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class MailHelper
1717
private const string NameGroup = "name";
1818
private const string EmailGroup = "email";
1919
private static readonly Regex Rfc2822Regex = new Regex(
20-
$@"(?:(?<{NameGroup}>)(?<{EmailGroup}>[^\<]*@.*[^\>])|(?<{NameGroup}>[^\<]*)\<(?<{EmailGroup}>.*@.*)\>)",
20+
$@"(?:(?<{NameGroup}>[^\<]*)\<(?<{EmailGroup}>.*@.*)\>|(?<{NameGroup}>)(?<{EmailGroup}>[^\<]*@.*[^\>]))",
2121
RegexOptions.ECMAScript);
2222

2323
/// <summary>

tests/SendGrid.Tests/Integration.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6126,6 +6126,18 @@ public async Task TestRetryBehaviourSucceedsOnSecondAttempt()
61266126
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
61276127
}
61286128

6129+
[Theory]
6130+
[InlineData("first last <[email protected]>", "first last", "[email protected]")]
6131+
[InlineData("<[email protected]>", "", "[email protected]")]
6132+
[InlineData("[email protected]", "", "[email protected]")]
6133+
6134+
public void TestStringToEmailAddress(string input, string expectedName, string expectedEmailAddress)
6135+
{
6136+
var actual = MailHelper.StringToEmailAddress(input);
6137+
6138+
Assert.True(actual.Name == expectedName && actual.Email == expectedEmailAddress);
6139+
}
6140+
61296141
/// <summary>
61306142
/// Tests the conditions in issue #670.
61316143
/// </summary>

0 commit comments

Comments
 (0)