Email address value object.


Build Status Coverage Status Downloads this Month Latest stable


Via Composer:

$ composer require nepada/email-address


This package provides two implementations of email address value object:

  1. RfcEmailAddress - it adheres to RFCs and treats local part of email address as case sensitive. The domain part is normalized to lower case ASCII representation.
  2. CaseInsensitiveEmailAddress - the only difference from RfcEmailAddress is that local part is considered case insensitive and normalized to lower case.

It is up to you to decide which implementation suites your needs. If you want to support both implementations, use Nepada\EmailAddress\EmailAddress as a typehint. You can also cast one representation to the other using RfcEmailAddress::toCaseInsensitiveEmailAddress() and CaseInsensitiveEmailAddress::toRfcEmailAddress().

Creating value object

$rfcEmailAddress = Nepada\EmailAddress\RfcEmailAddress::fromString('Real.example+suffix@HÁČKYčá');
$rfcEmailAddress = Nepada\EmailAddress\RfcEmailAddress::fromDomainAndLocalPart('HÁČKYčá', 'Real.example+suffix');

$ciEmailAddress = Nepada\EmailAddress\CaseInsensitiveEmailAddress::fromString('Real.example+suffix@HÁČKYčá');
$ciEmailAddress = Nepada\EmailAddress\CaseInsensitiveEmailAddress::fromDomainAndLocalPart('HÁČKYčá', 'Real.example+suffix');

Nepada\EmailAddress\InvalidEmailAddressException is thrown in case of invalid input value.

Converting back to string

Casting the value object to string, will result in the original (non-canonical) string representation of email address:

echo((string) $emailAddress); // Real.example+suffix@HÁČKYčá
echo($emailAddress->toString()); // Real.example+suffix@HÁČKYčá

Canonical string representation of email address

echo($emailAddress->getValue()); //

Getting normalized local and domain part separately

echo($emailAddress->getLocalPart()); // real.example+suffix
echo($emailAddress->getDomain()); //