Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Allow long email addresses per rfc 5322 #8

Merged
merged 1 commit into from
Jun 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/EmailAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class EmailAddress extends AbstractValidator
'useDeepMxCheck' => false,
'useDomainCheck' => true,
'allow' => Hostname::ALLOW_DNS,
'strict' => true,
'hostnameValidator' => null,
];

Expand All @@ -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
*
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand Down
23 changes: 23 additions & 0 deletions test/EmailAddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down