Validate a users email address when using gmail to register

I’ve only recently found out that when using a gmail account, you can add anything to the part before the @ and after the name and still only use the one email address.
Example.
myusername (at) gmail dot com
myusername+anythingelse (at) gmail dot com
Will result emails from both addresses going to the one gmail account.

Unfortunately when checking if email “myusername+anythingelse” exists using WP function “email_exists()”, WP I think rightly says it doesnt

As a result, I need to validate that the MAIN gmail account hasnt already been registered, plus if its not registered, let the user register with the extended email address.

Anyone give some help as to how to check if its a gmail account and then if necessary strip the part after+ so I check if the MAIN account email is registered to a user.

This is currently how I verify email addresses:

$validemail = is_email($email_to_check);
    if ($validemail):
        $exists = email_exists($email_to_check);
        if ($exists):
          Move on and verify

I sort of need help with this bit:

        elseif:
       is it a gmail account?
          if so, is it using a pseudonym +
             if so, split this , join the myusername and gmail dot com
                then recheck using email_exists()
                   if now it exists as true
                     return with warning, this email account is registered
                       else register email to user

1 Answer
1

Hmm, should try harder before asking for help 🙂

Was quite simple in the end (right or wrong) it works:

    $domain_array = array('googlemail.com','gmail.com');
    $validemail = is_email($email_to_check);
    $exists_text="";
    if ($validemail):
        $exists = email_exists($email_to_check);
        list($user, $domain) = explode('@', $email_to_check);
        if (!$exists):
            if (strpos($user, '+')) {
                list($username_email, $crap) = explode('+', $user);
                $exists = email_exists($username_email . '@' . $domain);
                $exists_text = __('User account exists with this email address', 'pw_texts');
                $email_to_check = strtolower($username_email . '@' . $domain);// Gmail only recognizes lowercase
            }

        endif;
        if ($exists):
           carry on as before

Leave a Comment