Advanced email pattern validation βοΈ
Specialized library for validating standard email formats with advanced provider filtering and validation features. π
- Introduction
- Key Features
- Installation
- Core Concepts
- Usage Guide
- API Reference
- Advanced Examples
- Comparison with Other Solutions
- Frequently Asked Questions
- Contribution and Support
- License
Mailcheck is a PHP library specialized in validating standard email formats, designed to go beyond the basic validation provided by native functions such as filter_var(). Developed for use cases that require strict validation of common email patterns, such as web forms, data cleaning pipelines, database filtering, and allowlist-based security systems.
The library allows you to validate email addresses based on three main criteria:
- Standard format - checks if the email follows the basic syntax
- Allowed providers - checks if the domain is in a list of trusted providers
- Domain extensions - checks if the email ends with an allowed extension (such as .com, .com.br)
Unlike generic validators, Mailcheck is deliberately restrictive and configurable, allowing you to define exactly which types of email addresses are valid for your specific use case.
- More restrictive validation than
filter_var()for common use cases β - Configurable allowlists for providers and domain extensions π
- Dual mode operation - static and instance methods for different code styles π
- Comprehensive sanitization utilities for input normalization π§Ή
- Batch processing capability for validating multiple emails π¦
- Complete customization of validation rules to meet specific requirements π§
- Unicode support for international emails with special characters π
- Optimized performance using compiled regular expressions β‘
- PHP 8.0 or higher
composer require pedrohrigolin/mailcheck- Download the
Mailcheck.phpfile from the GitHub repository - Include the file in your project:
require_once 'path/to/Mailcheck.php';To use as a library in CodeIgniter 3, uncomment the line:
// Remove the comment from this line
// defined('BASEPATH') OR exit('No direct script access allowed');Then place the file in the application/libraries/ folder of your CodeIgniter project.
Mailcheck has three main validation modes, which allow for increasing levels of verification:
- Validates only the basic email format
- Checks if the email follows the pattern
user@domain.extension - Similar to a basic syntax check, but more rigorous
// Validation in default mode
Mailcheck::checkStatic('user@domain.com'); // true for valid format- Validates the format and checks if the email ends with one of the allowed extensions
- By default, checks extensions like
.com,.com.br,.br - Useful for restricting emails to specific domain extensions
// Extension validation
Mailcheck::checkStatic('user@domain.com', Mailcheck::MODE_TERMINATION);- Validates the format and checks if the email uses one of the allowed providers
- By default, checks common providers like gmail.com, outlook.com, hotmail.com, etc.
- Ideal for applications that accept only emails from trusted providers
// Provider validation
Mailcheck::checkStatic('user@gmail.com', Mailcheck::MODE_PROVIDERS);Provider validation checks if the email domain is in the list of allowed providers. By default, this list includes common providers such as:
- gmail.com
- outlook.com/outlook.com.br
- hotmail.com/hotmail.com.br
- yahoo.com/yahoo.com.br
- live.com/live.com.br
- terra.com/terra.com.br
- icloud.com
- uol.com.br
- And others...
This list can be modified using the methods:
add_providers()/add_providersStatic()remove_providers()/remove_providersStatic()replace_providers()/replace_providersStatic()
Extension validation checks if the email ends with one of the allowed domain extensions. By default, it includes:
- .com
- .com.br
- .br
This list can be modified using the methods:
add_terminations()/add_terminationsStatic()remove_terminations()/remove_terminationsStatic()replace_terminations()/replace_terminationsStatic()
Mailcheck can be used in both static mode and as an instance.
// Validates a single email in default mode
if (Mailcheck::checkStatic('user@domain.com')) {
echo 'Valid email!';
}
// Validates a single email in extension mode
if (Mailcheck::checkStatic('user@domain.com', Mailcheck::MODE_TERMINATION)) {
echo 'Valid email with allowed extension!';
}
// Validates a single email in provider mode
if (Mailcheck::checkStatic('user@gmail.com', Mailcheck::MODE_PROVIDERS)) {
echo 'Valid email from an allowed provider!';
}
// You can also specify the mode as a string
if (Mailcheck::checkStatic('user@gmail.com', 'MODE_PROVIDERS')) {
echo 'Valid email from an allowed provider!';
}// Creates an instance of the class
$mailcheck = new Mailcheck();
// Validates a single email in default mode
if ($mailcheck->check('user@domain.com')) {
echo 'Valid email!';
}
// Validates a single email in extension mode
if ($mailcheck->check('user@domain.com', Mailcheck::MODE_TERMINATION)) {
echo 'Valid email with allowed extension!';
}
// Validates a single email in provider mode
if ($mailcheck->check('user@gmail.com', Mailcheck::MODE_PROVIDERS)) {
echo 'Valid email from an allowed provider!';
}To validate if an email uses a specific provider, use the MODE_PROVIDERS mode:
// Validates if the email uses an allowed provider
$email = 'user@gmail.com';
if (Mailcheck::checkStatic($email, Mailcheck::MODE_PROVIDERS)) {
echo "The email $email uses an allowed provider!";
}// Adds new providers to the existing list
Mailcheck::add_providersStatic('mydomain.com');
// or multiple at once
Mailcheck::add_providersStatic(['mydomain.com', 'company.com.br']);
// Removes providers from the list
Mailcheck::remove_providersStatic('hotmail.com');
// Replaces the entire provider list
Mailcheck::replace_providersStatic([
'company.com',
'company.com.br',
'partner.com'
]);
// Checks the current list of allowed providers
$providers = Mailcheck::getProviders();
print_r($providers);To validate if an email uses a specific extension, use the MODE_TERMINATION mode:
// Validates if the email uses an allowed extension
$email = 'user@domain.com';
if (Mailcheck::checkStatic($email, Mailcheck::MODE_TERMINATION)) {
echo "The email $email has an allowed extension!";
}// Adds new extensions to the existing list
Mailcheck::add_terminationsStatic('io');
// or multiple at once
Mailcheck::add_terminationsStatic(['io', 'tech'. 'io.tech']);
// Removes extensions from the list
Mailcheck::remove_terminationsStatic('br');
// Replaces the entire extension list
Mailcheck::replace_terminationsStatic([
'co',
'io',
'dev'
'co.io'
]);
// Checks the current list of allowed extensions
$extensions = Mailcheck::getTerminations();
print_r($extensions);Mailcheck provides functions to clean and normalize email addresses: π§Ή
// Sanitizes a single email
$clean_email = Mailcheck::sanitize(' user,,name@domain.com ');
// Result: "user.name@domain.com"
// Sanitizes and fixes multiple dots
$fixed_email = Mailcheck::sanitize('user...name@domain.com', true);
// Result: "user.name@domain.com"
// Sanitizes an array of emails
$emails = [
' user1@domain.com ',
'user2,,name@domain.com',
'user3...name@domain.com'
];
$clean_emails = Mailcheck::sanitize($emails, true);// Example 1: Basic sanitization with spaces and commas
$dirty_email = ' john,,doe@example.com ';
$clean_email = Mailcheck::sanitize($dirty_email);
echo "Original: '$dirty_email' β Sanitized: '$clean_email'";
// Output: Original: ' john,,doe@example.com ' β Sanitized: 'john.doe@example.com'
// Example 2: Fixing multiple dots
$dirty_email = 'maria....silva@company...com.br';
$clean_email = Mailcheck::sanitize($dirty_email, true);
echo "Original: '$dirty_email' β Sanitized: '$clean_email'";
// Output: Original: 'maria....silva@company...com.br' β Sanitized: 'maria.silva@company.com.br'
// Example 3: Sanitizing special characters
$dirty_email = 'carlos!#$%^&*()_+=@gmail.com';
$clean_email = Mailcheck::sanitize($dirty_email);
echo "Original: '$dirty_email' β Sanitized: '$clean_email'";
// Output: Original: 'carlos!#$%^&*()_+=@gmail.com' β Sanitized: 'carlos@gmail.com'
// Example 4: Sanitizing multiple emails with different results
$dirty_emails = [
' ana..silva@hotmail.com ',
'pedro,,santos@gmail...com',
'jose#ribeiro@uol.com.br',
'maria@example..com.br'
];
$clean_emails = Mailcheck::sanitize($dirty_emails, true);
echo "Batch sanitization results:\n";
foreach ($dirty_emails as $i => $original) {
echo "Original: '$original' β Sanitized: '{$clean_emails[$i]}'\n";
}
// Output:
// Batch sanitization results:
// Original: ' ana..silva@hotmail.com ' β Sanitized: 'ana.silva@hotmail.com'
// Original: 'pedro,,santos@gmail...com' β Sanitized: 'pedro.santos@gmail.com'
// Original: 'jose#ribeiro@uol.com.br' β Sanitized: 'joseribeiro@uol.com.br'
// Original: 'maria@example..com.br' β Sanitized: 'maria@example.com.br'Mailcheck allows you to validate and filter multiple emails at once:
// Checks if all emails in the array are valid
$emails = [
'user1@gmail.com',
'user2@outlook.com',
'user3@yahoo.com'
];
if (Mailcheck::checkStatic($emails, Mailcheck::MODE_PROVIDERS)) {
echo "All emails are valid and use allowed providers!";
} else {
echo "At least one of the emails is invalid or uses a non-allowed provider!";
}// List of emails to filter
$emails = [
'valid@gmail.com',
'invalid@unknown.domain',
'another@hotmail.com',
'test@nonexistent.provider'
];
// Filters to keep only valid emails (whitelist mode = true)
$valid_emails = Mailcheck::filterStatic($emails, Mailcheck::MODE_PROVIDERS);
// Filters to keep only invalid emails (whitelist mode = false)
$invalid_emails = Mailcheck::filterStatic($emails, Mailcheck::MODE_PROVIDERS, false);
// Using the instance version
$mailcheck = new Mailcheck();
$valid = $mailcheck->filter($emails, Mailcheck::MODE_PROVIDERS);// Diverse email list for demonstration
$mixed_emails = [
'maria@gmail.com',
'joao@outlook.com',
'roberto@invalid-domain.xyz',
'laura@hotmail.com',
'carlos@test-server.io',
'antonio@yahoo.com',
'invalid.email.com', // Missing @
'test@gmail.com',
'@domain.com', // Missing local part
'ana@uol.com.br'
];
// Example 1: Basic format filter
$valid_format = Mailcheck::filterStatic($mixed_emails, Mailcheck::MODE_DEFAULT);
echo "β
Emails with valid format (" . count($valid_format) . "):\n";
print_r($valid_format);
// Output:
// β
Emails with valid format (8):
// Array
// (
// [0] => maria@gmail.com
// [1] => joao@outlook.com
// [2] => roberto@invalid-domain.xyz
// [3] => laura@hotmail.com
// [4] => carlos@test-server.io
// [5] => antonio@yahoo.com
// [6] => test@gmail.com
// [7] => ana@uol.com.br
// )
// Example 2: Filter by allowed provider
$valid_providers = Mailcheck::filterStatic($mixed_emails, Mailcheck::MODE_PROVIDERS);
echo "β
Emails from allowed providers (" . count($valid_providers) . "):\n";
print_r($valid_providers);
// Output:
// β
Emails from allowed providers (6):
// Array
// (
// [0] => maria@gmail.com
// [1] => joao@outlook.com
// [2] => laura@hotmail.com
// [3] => antonio@yahoo.com
// [4] => test@gmail.com
// [5] => ana@uol.com.br
// )
// Example 3: Filter by extension
$valid_extensions = Mailcheck::filterStatic($mixed_emails, Mailcheck::MODE_TERMINATION);
echo "β
Emails with allowed extensions (" . count($valid_extensions) . "):\n";
print_r($valid_extensions);
// Output:
// β
Emails with allowed extensions (7):
// Array
// (
// [0] => maria@gmail.com
// [1] => joao@outlook.com
// [2] => laura@hotmail.com
// [3] => antonio@yahoo.com
// [4] => test@gmail.com
// [5] => ana@uol.com.br
// [6] => @domain.com
// )
// Example 4: Inverse filter (invalid emails)
$invalid_emails = Mailcheck::filterStatic($mixed_emails, Mailcheck::MODE_PROVIDERS, false);
echo "β Emails from non-allowed providers (" . count($invalid_emails) . "):\n";
print_r($invalid_emails);
// Output:
// β Emails from non-allowed providers (4):
// Array
// (
// [0] => roberto@invalid-domain.xyz
// [1] => carlos@test-server.io
// [2] => invalid.email.com
// [3] => @domain.com
// )
// Example 5: Combination of sanitization and filtering
$emails_to_clean = [
' maria..silva@gmail.com ',
'joao,,santos@outlook.com',
'user@invalid-domain.net',
'laura...santos@hotmail.com'
];
// First sanitize
$sanitized_emails = Mailcheck::sanitize($emails_to_clean, true);
// Then filter
$filtered_emails = Mailcheck::filterStatic($sanitized_emails, Mailcheck::MODE_PROVIDERS);
echo "π§Ή Emails after sanitization and filtering (" . count($filtered_emails) . "):\n";
print_r($filtered_emails);
// Output:
// π§Ή Emails after sanitization and filtering (3):
// Array
// (
// [0] => maria.silva@gmail.com
// [1] => joao.santos@outlook.com
// [2] => laura.santos@hotmail.com
// )Validates an email or array of emails in static mode.
- Parameters:
$email: String with the email or array of emails to validate$type: Validation mode (0/MODE_DEFAULT, 1/MODE_TERMINATION, 2/MODE_PROVIDERS)
- Return: Boolean - true if valid, false if invalid
- Example:
$is_valid = Mailcheck::checkStatic('user@gmail.com', Mailcheck::MODE_PROVIDERS);
Validates an email or array of emails using an instance.
- Parameters:
$email: String with the email or array of emails to validate$type: Validation mode (0/MODE_DEFAULT, 1/MODE_TERMINATION, 2/MODE_PROVIDERS)
- Return: Boolean - true if valid, false if invalid
- Example:
$mailcheck = new Mailcheck(); $is_valid = $mailcheck->check('user@gmail.com', Mailcheck::MODE_PROVIDERS);
Filters an array of emails keeping or removing the valid ones (static mode).
- Parameters:
$email: Array of emails to filter$type: Validation mode (0/MODE_DEFAULT, 1/MODE_TERMINATION, 2/MODE_PROVIDERS)$white_list: true to keep valid ones, false to keep invalid ones
- Return: Array of filtered emails
- Example:
$valid_emails = Mailcheck::filterStatic($emails, Mailcheck::MODE_PROVIDERS);
Filters an array of emails keeping or removing the valid ones (instance mode).
- Parameters:
$email: Array of emails to filter$type: Validation mode (0/MODE_DEFAULT, 1/MODE_TERMINATION, 2/MODE_PROVIDERS)$white_list: true to keep valid ones, false to keep invalid ones
- Return: Array of filtered emails
- Example:
$mailcheck = new Mailcheck(); $valid_emails = $mailcheck->filter($emails, Mailcheck::MODE_PROVIDERS);
Normalizes the email format(s), removing spaces, replacing commas with dots, etc.
- Parameters:
$email: String with the email or array of emails to sanitize$fixDots: If true, fixes multiple dots (e.g.,user...name@domain.comβuser.name@domain.com)
- Return: String or array with sanitized email(s)
- Example:
$clean_email = Mailcheck::sanitize(' user,,name@domain.com '); // Result: "user.name@domain.com"
Adds one or more providers to the allowlist (static mode).
- Parameters:
$providers: String with one provider or array of providers to add
- Example:
Mailcheck::add_providersStatic('mydomain.com'); // or Mailcheck::add_providersStatic(['mydomain.com', 'company.com.br']);
Adds one or more providers to the allowlist (instance mode).
- Parameters:
$providers: String with one provider or array of providers to add
- Example:
$mailcheck = new Mailcheck(); $mailcheck->add_providers('mydomain.com');
Removes one or more providers from the allowlist (static mode).
- Parameters:
$providers: String with one provider or array of providers to remove
- Example:
Mailcheck::remove_providersStatic('hotmail.com'); // or Mailcheck::remove_providersStatic(['hotmail.com', 'outlook.com']);
Removes one or more providers from the allowlist (instance mode).
- Parameters:
$providers: String with one provider or array of providers to remove
- Example:
$mailcheck = new Mailcheck(); $mailcheck->remove_providers('hotmail.com');
Replaces the entire list of allowed providers (static mode).
- Parameters:
$providers: Array with the new allowed providers
- Example:
Mailcheck::replace_providersStatic(['company.com', 'company.com.br']);
Replaces the entire list of allowed providers (instance mode).
- Parameters:
$providers: Array with the new allowed providers
- Example:
$mailcheck = new Mailcheck(); $mailcheck->replace_providers(['company.com', 'company.com.br']);
Adds one or more extensions to the allowlist (static mode).
- Parameters:
$terminations: String with one extension or array of extensions to add
- Example:
Mailcheck::add_terminationsStatic('io'); // or Mailcheck::add_terminationsStatic(['io', 'tech']);
Adds one or more extensions to the allowlist (instance mode).
- Parameters:
$terminations: String with one extension or array of extensions to add
- Example:
$mailcheck = new Mailcheck(); $mailcheck->add_terminations('io');
Removes one or more extensions from the allowlist (static mode).
- Parameters:
$terminations: String with one extension or array of extensions to remove
- Example:
Mailcheck::remove_terminationsStatic('br'); // or Mailcheck::remove_terminationsStatic(['br', 'com.br']);
Removes one or more extensions from the allowlist (instance mode).
- Parameters:
$terminations: String with one extension or array of extensions to remove
- Example:
$mailcheck = new Mailcheck(); $mailcheck->remove_terminations('br');
Replaces the entire list of allowed extensions (static mode).
- Parameters:
$terminations: Array with the new allowed extensions
- Example:
Mailcheck::replace_terminationsStatic(['co', 'io', 'dev']);
Replaces the entire list of allowed extensions (instance mode).
- Parameters:
$terminations: Array with the new allowed extensions
- Example:
$mailcheck = new Mailcheck(); $mailcheck->replace_terminations(['co', 'io', 'dev']);
Returns the current list of allowed providers.
- Return: Array with the allowed providers
- Example:
$providers = Mailcheck::getProviders(); print_r($providers);
Returns the current list of allowed extensions.
- Return: Array with the allowed extensions
- Example:
$terminations = Mailcheck::getTerminations(); print_r($terminations);
Returns the available validation mode constants.
- Return: Array with the available validation modes
- Example:
$modes = Mailcheck::getModes(); print_r($modes);
// Processes contact form
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = $_POST['email'] ?? '';
// Sanitizes and validates the email
$email = Mailcheck::sanitize($email, true);
if (Mailcheck::checkStatic($email, Mailcheck::MODE_PROVIDERS)) {
// Valid email from an allowed provider
send_email($email);
echo "Form submitted successfully!";
} else {
echo "Please provide a valid email address from a trusted provider.";
}
}class UserRegistration {
private $mailcheck;
public function __construct() {
$this->mailcheck = new Mailcheck();
// Configure to allow only corporate domains
$this->mailcheck->replace_providers([
'mycompany.com',
'mycompany.com.br',
'partner.com'
]);
}
public function validateEmail($email) {
$email = $this->mailcheck->sanitize($email, true);
return $this->mailcheck->check($email, Mailcheck::MODE_PROVIDERS);
}
public function register($name, $email, $password) {
if (!$this->validateEmail($email)) {
return "Invalid email. Please use your corporate email.";
}
// Continue with registration...
return "User registered successfully!";
}
}
// Usage
$registration = new UserRegistration();
echo $registration->register('John', 'john@mycompany.com', 'password123');function import_contacts($csv_file) {
// Load CSV
$contacts = [];
if (($handle = fopen($csv_file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$contacts[] = [
'name' => $data[0],
'email' => $data[1]
];
}
fclose($handle);
}
// Extract only the emails
$emails = array_column($contacts, 'email');
// Sanitize all emails
$emails = Mailcheck::sanitize($emails, true);
// Separate into valid and invalid
$valid = Mailcheck::filterStatic($emails, Mailcheck::MODE_DEFAULT);
$invalid = Mailcheck::filterStatic($emails, Mailcheck::MODE_DEFAULT, false);
// Separate by specific providers
Mailcheck::replace_providersStatic(['gmail.com', 'hotmail.com']);
$gmail_hotmail = Mailcheck::filterStatic($valid, Mailcheck::MODE_PROVIDERS);
$other_providers = Mailcheck::filterStatic($valid, Mailcheck::MODE_PROVIDERS, false);
return [
'total' => count($emails),
'valid' => $valid,
'invalid' => $invalid,
'gmail_hotmail' => $gmail_hotmail,
'others' => $other_providers
];
}
// Usage
$result = import_contacts('contacts.csv');
echo "Total contacts: " . $result['total'] . "\n";
echo "Valid emails: " . count($result['valid']) . "\n";
echo "Invalid emails: " . count($result['invalid']) . "\n";| Feature | Mailcheck | filter_var | Simple regex validation |
|---|---|---|---|
| Basic validation | β | β | β |
| Strict format validation | β | β | |
| Provider validation | β | β | β |
| Extension validation | β | β | |
| Allowlists | β | β | β |
| Batch processing | β | ||
| Sanitization | β | β | β |
| Rule customization | β | β | |
| Unicode support | β | β (complex) |
filter_var() is a generic function for validating whether an email follows the basic format, but it doesn't provide more specific validation such as checking allowed providers or domain extensions. Mailcheck was specifically designed for more restrictive and customizable validations.
Yes, Mailcheck supports Unicode characters (\p{L}) in regular expressions, allowing emails with international characters.
Mailcheck::add_providersStatic('mycompany.com');
// or for multiple corporate domains
Mailcheck::replace_providersStatic([
'mycompany.com',
'department.mycompany.com',
'branch.mycompany.com'
]);No, Mailcheck focuses on format validation and verification against allowlists. For DNS checks that confirm the actual existence of the domain, you would need to implement an additional solution using functions like checkdnsrr().
Mailcheck focuses on format validation and verification against allowlists. To verify if an email actually exists, consider:
- Integration with email verification services
- DNS verification to confirm MX records
- Implementation of email confirmation (sending a link or code)