PHP – Gmail Address Normalization

An email address from Google’s Gmail can be transformed in various ways into different looking email addresses while still going to the same account. A Gmail user can do the following to have multiple email addresses:

  • Using a plus address: gmail+test@gmail.com is really gmail@gmail.com
  • Changing the host to googlemail.com; gmail@googlemail.com is gmail@gmail.com
  • Adding additional dots to the name: g.mail@gmail.com still goes to gmail@gmail.com

To see which Gmail email addresses belong to the same account, run them through gmail_address() and verify if any of the results are

function gmail_address($mail) {
  $mail_part = explode(‘@’, strtolower($mail));
  if ($mail_part[1] !== ‘gmail.com’ && $mail_part[1] !== ‘googlemail.com’) {
    return $mail;
  }
  $plus_position = strpos($mail_part[0], ‘+’);
  if ($plus_position !== false) {
    $mail_part[0] = substr($mail_part[0], 0, $plus_position);
  }
  return str_replace(‘.’, ”, $mail_part[0]) . ‘@gmail.com’;
}

Examples of usage:

echo gmail_address(‘gmail@googlemail.com’); // gmail@gmail.com
echo gmail_address(‘gm.Bil@gmail.com’);     // gmail@gmail.com
echo gmail_address(‘gmail+test@gmail.com’); // gmail@gmail.com

Other email addresses are returned unmodified.

Leave a Reply

Your email address will not be published. Required fields are marked *

*