Simple PHP library to canonize email addresses from gmail.com, outlook.com or other providers that allow several forms of email.
Be careful: do not store canonical email as primary email for login or sending emails!
Your users may not be able to login again to your site if they used a specific email syntax which differs from canonical. Only store canonical emails in order to test against duplicates and prevent new users from creating multiple accounts with same email using variants.
Always store email
and canonical_email
in your databases.
LowercaseDomainStrategy
: for every email, domain is case-insensitive, so it should be lowercased.GmailStrategy
: for@gmail.com
addresses or whatever domain which MX servers are from Google GSuite (if$checkMxRecords
istrue
). This will remove any dots, and any character after+
sign. Then all email parts will be lowercased. When MX are checked, your app will use PHPgetmxrr
function.OutlookStrategy
: for@outlook.com
addresses. This will remove any character after+
sign.
composer require rezozero/canonical-email
use RZ\CanonicalEmail\EmailCanonizer;
use RZ\CanonicalEmail\Strategy\GmailStrategy;
use RZ\CanonicalEmail\Strategy\GSuiteStrategy;
use RZ\CanonicalEmail\Strategy\LowercaseDomainStrategy;
use RZ\CanonicalEmail\Strategy\OutlookStrategy;
$canonizer = new EmailCanonizer([
new LowercaseDomainStrategy(),
new GmailStrategy(),
new GSuiteStrategy(),
new OutlookStrategy()
]);
$canonizer->getCanonicalEmailAddress($email);
composer install
make test