diff --git a/composer.json b/composer.json index f9e25e347..313230c9d 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,7 @@ ], "require": { "php": ">=5.6", + "egulias/email-validator": "^2.0", "sendgrid/php-http-client": "~3.10", "starkbank/ecdsa": "0.*", "ext-curl": "*", diff --git a/lib/helper/Assert.php b/lib/helper/Assert.php index f1da609a7..b823ac1d1 100644 --- a/lib/helper/Assert.php +++ b/lib/helper/Assert.php @@ -8,6 +8,8 @@ namespace SendGrid\Helper; +use Egulias\EmailValidator\EmailLexer; +use Egulias\EmailValidator\Validation\RFCValidation; use SendGrid\Mail\TypeException; class Assert @@ -47,11 +49,7 @@ public static function email($value, $property, $message = null) { static::string($value, $property, $message); - // Define additional flags for filter_var to verify unicode characters on local part - // Constant FILTER_FLAG_EMAIL_UNICODE is available since PHP 7.1 - $flags = (defined('FILTER_FLAG_EMAIL_UNICODE')) ? FILTER_FLAG_EMAIL_UNICODE : null; - - if (filter_var($value, FILTER_VALIDATE_EMAIL, $flags) === false) { + if ((new RFCValidation())->isValid($value, new EmailLexer()) === false) { $message = sprintf( $message ?: '"$%s" must be a valid email address. Got: %s', $property, diff --git a/test/unit/AssertTest.php b/test/unit/AssertTest.php index 313f325c3..4faca0c82 100644 --- a/test/unit/AssertTest.php +++ b/test/unit/AssertTest.php @@ -41,6 +41,14 @@ public function testEmailThrowExceptionWithDefaultMessage() Assert::email('test', 'test'); } + public function testEmailThrowExceptionWithNonRFCFormatWithDefaultMessage() + { + $this->expectException(TypeException::class); + $this->expectExceptionMessage('"$test" must be a valid email address. Got: test'); + + Assert::email('test sd@test.es', 'test'); + } + public function testEmailThrowExceptionWithCustomMessage() { $this->expectException(TypeException::class);