PHP code example of stymiee / email-validator

1. Go to this page and download the library: Download stymiee/email-validator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stymiee / email-validator example snippets


$config = [
    'checkMxRecords' => true,
    'checkBannedListedEmail' => true,
    'checkDisposableEmail' => true,
    'checkFreeEmail' => true,
    'bannedList' => $bannedDomainList,
    'disposableList' => $customDisposableEmailList,
    'freeList' => $customFreeEmailList,
];
$emailValidator = new EmailValidator($config);



namespace EmailValidator;

   '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;
}

use EmailValidator\Validator\AValidator;
use EmailValidator\EmailAddress;
use EmailValidator\Policy;

class MyCustomValidator extends AValidator
{
    public function validate(EmailAddress $email): bool
    {
        // Your custom validation logic here
        return $email->getDomain() === 'example.com';
    }
}

// Register your custom validator
$emailValidator = new EmailValidator();
$emailValidator->registerValidator(new MyCustomValidator(new Policy()));

// Use it like any other validator
$isValid = $emailValidator->validate('[email protected]');



namespace EmailValidator;

 => true,
    'useRfc5322' => true  // Enable RFC 5322 validation
];
$emailValidator = new EmailValidator($config);

$testEmailAddresses = [
    // Standard email addresses
    '[email protected]',
    
    // Quoted strings
    '"John Doe"@example.com',
    '"[email protected]"@example.com',
    
    // Comments
    'user(comment)@example.com',
    'user@(comment)example.com',
    
    // Domain literals
    'user@[192.0.2.1]',
    'user@[IPv6:2001:db8::1]',
    
    // International domains
    'user@münchen.de',
    '[email protected]'  // Punycode
];

foreach ($testEmailAddresses as $emailAddress) {
    $emailIsValid = $emailValidator->validate($emailAddress);
    echo ($emailIsValid) ? 'Email is valid' : $emailValidator->getErrorReason();
    echo PHP_EOL;
}