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

Commit

Permalink
Add support for internationalized email address local part
Browse files Browse the repository at this point in the history
  • Loading branch information
Xerkus committed Apr 5, 2017
1 parent 55e55e0 commit 1a0ffdb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/EmailAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Zend\Validator;

use UConverter;

class EmailAddress extends AbstractValidator
{
const INVALID = 'emailAddressInvalid';
Expand Down Expand Up @@ -342,6 +344,8 @@ protected function validateLocalPart()
$atext = 'a-zA-Z0-9\x21\x23\x24\x25\x26\x27\x2a\x2b\x2d\x2f\x3d\x3f\x5e\x5f\x60\x7b\x7c\x7d\x7e';
if (preg_match('/^[' . $atext . ']+(\x2e+[' . $atext . ']+)*$/', $this->localPart)) {
$result = true;
} elseif ($this->validateInternationalizedLocalPart()) {
$result = true;
} else {
// Try quoted string format (RFC 5321 Chapter 4.1.2)

Expand All @@ -360,6 +364,22 @@ protected function validateLocalPart()
return $result;
}

protected function validateInternationalizedLocalPart()
{
if (
extension_loaded('intl')
&& false === UConverter::transcode($this->localPart, 'UTF-8', 'UTF-8')
) {
// invalid utf?
return false;
}
$atext = 'a-zA-Z0-9\x21\x23\x24\x25\x26\x27\x2a\x2b\x2d\x2f\x3d\x3f\x5e\x5f\x60\x7b\x7c\x7d\x7e';
// RFC 6532 extends atext to include non-ascii utf
// @see https://tools.ietf.org/html/rfc6532#section-3.1
$uatext = $atext . '\x{80}-\x{FFFF}';
return (bool) preg_match('/^[' . $uatext . ']+(\x2e+[' . $uatext . ']+)*$/u', $this->localPart);
}

/**
* Returns the found MX Record information after validation including weight for further processing
*
Expand Down
4 changes: 3 additions & 1 deletion test/EmailAddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ public function validEmailAddresses()
];

if (extension_loaded('intl')) {
$return['иван@письмо.рф'] = ['иван@письмо.рф'];
$return['öäü@ä-umlaut.de'] = ['öäü@ä-umlaut.de'];
$return['frédé[email protected]'] = ['frédé[email protected]'];
$return['bob@тест.рф'] = ['bob@тест.рф'];
$return['[email protected]'] = ['[email protected]'];
}
Expand Down Expand Up @@ -277,7 +280,6 @@ public function invalidEmailAddresses()
'bob @ domain.com' => ['bob @ domain.com'],
'[email protected]' => ['[email protected]'],
'"bob%[email protected]' => ['"bob%[email protected]'],
'иван@письмо.рф' => ['иван@письмо.рф'],
'multiline' => ['bob
@domain.com'],
Expand Down

0 comments on commit 1a0ffdb

Please sign in to comment.