From b55aaa0c44587c0440f8f7c4d591b6dd2ffb70d5 Mon Sep 17 00:00:00 2001 From: Denis Sokolov Date: Wed, 10 Jun 2015 11:54:23 +0300 Subject: [PATCH] Allow long email addresses per rfc 5321 --- src/EmailAddress.php | 12 ++++++++++-- test/EmailAddressTest.php | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/EmailAddress.php b/src/EmailAddress.php index 61242a48f..b477185bd 100644 --- a/src/EmailAddress.php +++ b/src/EmailAddress.php @@ -69,6 +69,7 @@ class EmailAddress extends AbstractValidator 'useDeepMxCheck' => false, 'useDomainCheck' => true, 'allow' => Hostname::ALLOW_DNS, + 'strict' => true, 'hostnameValidator' => null, ]; @@ -78,6 +79,7 @@ class EmailAddress extends AbstractValidator * The following additional option keys are supported: * 'hostnameValidator' => A hostname validator, see Zend\Validator\Hostname * 'allow' => Options for the hostname validator, see Zend\Validator\Hostname::ALLOW_* + * 'strict' => Whether to adhere to strictest requirements in the spec * 'useMxCheck' => If MX check should be enabled, boolean * 'useDeepMxCheck' => If a deep MX check should be done, boolean * @@ -499,7 +501,7 @@ public function isValid($value) return false; } - if ((strlen($this->localPart) > 64) || (strlen($this->hostname) > 255)) { + if ($this->getOption('strict') && (strlen($this->localPart) > 64) || (strlen($this->hostname) > 255)) { $length = false; $this->error(self::LENGTH_EXCEEDED); } @@ -542,7 +544,13 @@ protected function idnToAscii($email) protected function idnToUtf8($email) { if (extension_loaded('intl')) { - return idn_to_utf8($email); + // The documentation does not clarify what kind of failure + // can happen in idn_to_utf8. One can assume if the source + // is not IDN encoded, it would fail, but it usually returns + // the source string in those cases. + // But not when the source string is long enough. + // Thus we default to source string ourselves. + return idn_to_utf8($email) ?: $email; } return $email; } diff --git a/test/EmailAddressTest.php b/test/EmailAddressTest.php index 154ef396a..c0cd9fa77 100644 --- a/test/EmailAddressTest.php +++ b/test/EmailAddressTest.php @@ -293,6 +293,29 @@ public function testComplexLocalValid() } } + /** + * Ensures that the validator follows expected behavior for valid email addresses with the non-strict option + * + * @return void + */ + public function testNonStrict() + { + $validator = new EmailAddress(['strict' => false]); + $emailAddresses = [ + // RFC 5321 does mention a limit of 64 for the username, + // but it also states "To the maximum extent possible, + // implementation techniques that impose no limits on the + // length of these objects should be used.". + // http://tools.ietf.org/html/rfc5321#section-4.5.3.1 + 'line length 320' => str_repeat('x', 309).'@domain.com', + 'line length 321' => str_repeat('x', 310).'@domain.com', + 'line length 911' => str_repeat('x', 900).'@domain.com', + ]; + foreach ($emailAddresses as $input) { + $this->assertTrue($validator->isValid($input)); + } + } + /** * Ensures that the validator follows expected behavior for checking MX records *