Skip to content

Instantly share code, notes, and snippets.

@judge2020
Last active February 6, 2019 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save judge2020/af8fb9cd2ac165462d44de4e58d9a509 to your computer and use it in GitHub Desktop.
Save judge2020/af8fb9cd2ac165462d44de4e58d9a509 to your computer and use it in GitHub Desktop.

"normalizes" an email to prevent multiple accounts being created for each email inbox. This would be ran on both register and login.

We don't DNS lookup gsuite because gsuite does not have the gmail "plus address" feature.

License:

Copyright 2019 github.com/judge2020

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

function normalizeEmail($email)
{
if (strpos($email, '@') === false)
return $email;
$e = explode('@', $email);
$user = $e[0];
$domain = $e[1];
if (isGmail($email)) {
if ($domain == 'googlemail.com')
$domain = 'gmail.com';
$user = str_replace('.', '', $user);
if (strpos($user, '+') !== false)
$user = substr($user, 0, strpos($user, '+'));
} elseif (isOutlook($email)) {
if (strpos($user, '+') !== false)
$user = substr($user, 0, strpos($user, '+'));
} elseif (isYahoo($email)) {
if (strpos($user, '-') !== false)
$user = substr($user, 0, strpos($user, '-'));
}
return $user . '@' . $domain;
}
function isOutlook($email)
{
if (strpos($email, '@') === false)
return false;
$e = explode('@', $email);
$mx = null;
dns_get_mx($e[1], $mx);
if ($mx == null || !is_array($mx) || count($mx) <= 0)
return false;
return strpos($mx[0], 'outlook.com') !== false;
}
function isGmail($email)
{
if (strpos($email, '@') === false)
return false;
$e = explode('@', $email);
return in_array($e[1], ['gmail.com', 'googlemail.com']);
}
function isYahoo($email)
{
if (strpos($email, '@') === false)
return false;
$e = explode('@', $email);
$mx = null;
dns_get_mx($e[1], $mx);
if ($mx == null || !is_array($mx) || count($mx) <= 0)
return false;
return strpos($mx[0], 'yahoodns.net') !== false || strpos($mx[0], 'yahoo.com') !== false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment