When using emails as usernames you want them to be unique over your table, but this can be a problem if you consider a GMail account, because of their username policy. They allow:
- dots
- digits
- letters
- plus addressing text by using the
+
sign, i.e.orange+juice@gmail.com
- length between 6 and 30 characters, excluding dots and the appended part
But when resolving the username they do not consider:
- dots
- different capitalization
- plus addressing
So, when you write to:
UserName@gmail.com
u.sername@gmail.com
user.name+forum@gmail.com
.u.s.e.r.n.a.m.e.@gmail.com
u.serName+doh@googlemail.com
You will always match the same account:
username@gmail.com
This class can help to define if the submitted email is a valid GMail address and to get the basic version: when using the parseMail()
method it will just validate emails from other providers, so that you can submit an array of emails and get in return all the basics versions of GMail and all the other emails. By submitting an array to the isGmail()
method, instead, you will get only an array of valid GMail accounts, in their basic version.
I'm applying lowercase to all the emails, the RFC 2821 allows case sensitive local-parts, but this is discouraged. Some examples:
<?php
$list = array(
'user.name@anemail.com',
'username+acme@gmail.com',
'email' => 'another@gmail.com',
array(
'test@ymail.com',
'will+fail@gmail.com',
'this032@googlemail.com',
'"valid@test"@email.com',
'Awesome@yahoo.com'
),
'someone+doh@gmail.com',
'AnotherUser+focus@gmail.com',
'simple@gmail.com'
);
$gm = new GMailParser;
# testing strings
var_dump($gm->isGmail('user.name@amail.com'));
var_dump($gm->isGMail('user.name@gmail.com'));
/*
bool(false)
bool(true)
*/
var_dump($gm->parseMail('user.name@amail.com'));
var_dump($gm->parseMail('user.name@gmail.com'));
/*
string(19) "user.name@amail.com"
string(18) "username@gmail.com"
*/
# testing arrays
print_r($gm->isGmail($list));
/*
Array
(
[0] => username@gmail.com
[1] => another@gmail.com
[2] => this032@gmail.com
[3] => someone@gmail.com
[4] => anotheruser@gmail.com
[5] => simple@gmail.com
)
*/
print_r($gm->parseMail($list));
/*
Array
(
[0] => user.name@anemail.com
[1] => username@gmail.com
[2] => another@gmail.com
[3] => test@ymail.com
[4] => this032@googlemail.com
[5] => "valid@test"@email.com
[6] => awesome@yahoo.com
[7] => someone@gmail.com
[8] => anotheruser@gmail.com
[9] => simple@gmail.com
)
*/
I use this to save the basic version of the submitted GMail address. I also tend to save the version written by the user to mantain the plus addressing text, which is used to create filters in the GMail application:
username+forum@gmail.com
So that I can allow the user to login with any version of his email, it will always match the same account, but I can always send the email to the original version.