-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for identifying and working with Gmail addresses using …
…the "plus trick" to create unique addresses
- Loading branch information
Showing
7 changed files
with
225 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ The PHP Email Validator will validate an email address for all or some of the fo | |
- is not a disposable email address (optional) | ||
- is not a free email account (optional) | ||
- is not a banned email domain (optional) | ||
- flag Gmail accounts that use the "plus trick" and return a sanitized email address | ||
|
||
The Email Validator is configurable, so you have full control over how much validation will occur. | ||
|
||
|
@@ -27,13 +28,13 @@ Simply add a dependency on `stymiee/email-validator` to your project's `composer | |
[Composer](https://getcomposer.org/) to manage the dependencies of your project. | ||
|
||
Here is a minimal example of a `composer.json` file that just defines a dependency on PHP Simple Encryption: | ||
|
||
{ | ||
"require": { | ||
"stymiee/email-validator": "^1" | ||
} | ||
```json | ||
{ | ||
"require": { | ||
"stymiee/email-validator": "^1" | ||
} | ||
|
||
} | ||
``` | ||
## Functional Description | ||
|
||
The Email Validator library builds upon PHP's built in `filter_var($emailAddress, FILTER_VALIDATE_EMAIL);` by adding a | ||
|
@@ -73,6 +74,15 @@ domains (i.e. public email providers like Gmail or Yahoo mail), you can block th | |
to `true` in the configuration (see below) and providing an array of banned domains. Examples are provided in the | ||
`examples` directory which demonstrate how to do this. | ||
|
||
### Flag Gmail Addresses Using The "Plus Trick" | ||
|
||
Gmail offers the ability to create unique email addresses within a Google account by adding a `+` character and unique | ||
identifier after the username portion of the email address. If not explicitly checked for a user can create an unlimited | ||
amount of unique email addresses that all belong to the same account. | ||
|
||
A special check can be performed when a Gmail account is used and a sanitized email address (e.g. one without the "plus | ||
trick") can be obtained and then checked for uniqueness in your system. | ||
|
||
### Configuration | ||
|
||
To configure the Email Validator you can pass an array with the follow parameters/values: | ||
|
@@ -116,81 +126,96 @@ An array of domains that are suspected disposable email address providers. | |
An array of domains that are free email address providers. | ||
|
||
**Example** | ||
|
||
$config = [ | ||
'checkMxRecords' => true, | ||
'checkBannedListedEmail' => true, | ||
'checkDisposableEmail' => true, | ||
'checkFreeEmail' => true, | ||
'bannedList' => $bannedDomainList, | ||
'disposableList' => $customDisposableEmailList, | ||
'freeList' => $customFreeEmailList, | ||
]; | ||
$emailValidator = new EmailValidator($config); | ||
|
||
```php | ||
$config = [ | ||
'checkMxRecords' => true, | ||
'checkBannedListedEmail' => true, | ||
'checkDisposableEmail' => true, | ||
'checkFreeEmail' => true, | ||
'bannedList' => $bannedDomainList, | ||
'disposableList' => $customDisposableEmailList, | ||
'freeList' => $customFreeEmailList, | ||
]; | ||
$emailValidator = new EmailValidator($config); | ||
```` | ||
### Example | ||
|
||
<?php | ||
use EmailValidator\EmailValidator; | ||
|
||
require('../vendor/autoload.php'); | ||
|
||
$customDisposableEmailList = [ | ||
'example.com', | ||
]; | ||
$customFreeEmailList = [ | ||
'example2.com', | ||
]; | ||
$bannedDomainList = [ | ||
'domain.com', | ||
]; | ||
$testEmailAddresses = [ | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
]; | ||
|
||
$config = [ | ||
'checkMxRecords' => true, | ||
'checkBannedListedEmail' => true, | ||
'checkDisposableEmail' => true, | ||
'checkFreeEmail' => true, | ||
'bannedList' => $bannedDomainList, | ||
'disposableList' => $customDisposableEmailList, | ||
'freeList' => $customFreeEmailList, | ||
]; | ||
$emailValidator = new EmailValidator($config); | ||
|
||
foreach($testEmailAddresses as $emailAddress) { | ||
$emailIsValid = $emailValidator->validate($emailAddress); | ||
echo ($emailIsValid) ? 'Email is valid' : $emailValidator->getErrorReason(); | ||
echo PHP_EOL; | ||
```php | ||
<?php | ||
|
||
namespace EmailValidator; | ||
|
||
require('../vendor/autoload.php'); | ||
|
||
$customDisposableEmailList = [ | ||
'example.com', | ||
]; | ||
|
||
$bannedDomainList = [ | ||
'domain.com', | ||
]; | ||
|
||
$customFreeEmailList = [ | ||
'example2.com', | ||
]; | ||
|
||
$testEmailAddresses = [ | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
]; | ||
|
||
$config = [ | ||
'checkMxRecords' => true, | ||
'checkBannedListedEmail' => true, | ||
'checkDisposableEmail' => true, | ||
'checkFreeEmail' => true, | ||
'bannedList' => $bannedDomainList, | ||
'disposableList' => $customDisposableEmailList, | ||
'freeList' => $customFreeEmailList, | ||
]; | ||
$emailValidator = new EmailValidator($config); | ||
|
||
foreach ($testEmailAddresses as $emailAddress) { | ||
$emailIsValid = $emailValidator->validate($emailAddress); | ||
echo ($emailIsValid) ? 'Email is valid' : $emailValidator->getErrorReason(); | ||
if ($emailValidator->isGmailWithPlusChar()) { | ||
printf( | ||
' (Sanitized address: %s)', | ||
$emailValidator->getGmailAddressWithoutPlus() | ||
); | ||
} | ||
echo PHP_EOL; | ||
} | ||
``` | ||
|
||
**Output** | ||
|
||
Email is valid | ||
Domain is used by free email providers | ||
Domain is used by free email providers | ||
Domain is used by free email providers | ||
Domain is used by free email providers | ||
Domain is banned | ||
Domain is used by disposable email providers | ||
Domain is used by free email providers | ||
Domain is used by disposable email providers | ||
Domain does not accept email | ||
Domain is used by disposable email providers | ||
Domain is used by disposable email providers | ||
|
||
``` | ||
Domain is banned | ||
Email is valid | ||
Domain is used by free email providers | ||
Domain is used by free email providers | ||
Domain is used by free email providers | ||
Domain is used by free email providers | ||
Domain is banned | ||
Domain does not accept email | ||
Domain is used by disposable email providers | ||
Domain is used by free email providers | ||
Domain is used by disposable email providers | ||
Domain does not accept email | ||
Domain is used by disposable email providers | ||
Domain is used by free email providers (Sanitized address: [email protected]) | ||
``` | ||
## Notes | ||
|
||
The email address is checked against a list of known disposable email address providers which are aggregated from | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ | |
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
]; | ||
|
||
$config = [ | ||
|
@@ -46,5 +47,12 @@ | |
foreach ($testEmailAddresses as $emailAddress) { | ||
$emailIsValid = $emailValidator->validate($emailAddress); | ||
echo ($emailIsValid) ? 'Email is valid' : $emailValidator->getErrorReason(); | ||
if ($emailValidator->isGmailWithPlusChar()) { | ||
printf( | ||
' (%s is a Gmail account and contains a plus character. Sanitized address: %s)', | ||
$emailAddress, | ||
$emailValidator->getGmailAddressWithoutPlus() | ||
); | ||
} | ||
echo PHP_EOL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,4 +52,31 @@ public function testGetEmailAddress(string $emailAddress): void | |
$email = new EmailAddress($emailAddress); | ||
self::assertEquals($email->getEmailAddress(), $emailAddress); | ||
} | ||
|
||
public function emailWithPlusDataProvider(): array | ||
{ | ||
return [ | ||
['[email protected]', false], | ||
['[email protected]', true], | ||
['[email protected]', false], | ||
['[email protected]', false], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider emailWithPlusDataProvider | ||
* @param string $emailAddress | ||
* @param bool $hasPlus | ||
*/ | ||
public function testIsGmailWithPlusChar(string $emailAddress, bool $hasPlus): void | ||
{ | ||
$email = new EmailAddress($emailAddress); | ||
self::assertEquals($hasPlus, $email->isGmailWithPlusChar()); | ||
} | ||
|
||
public function testGetGmailAddressWithoutPlus(): void | ||
{ | ||
$email = new EmailAddress('[email protected]'); | ||
self::assertEquals('[email protected]', $email->getGmailAddressWithoutPlus()); | ||
} | ||
} |
Oops, something went wrong.