Convert Figma logo to code with AI

egulias logoEmailValidator

PHP Email address validator

11,403
206
11,403
30

Top Related Projects

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)

Provides tools to validate values

Reduce misspelled email addresses in your web apps.

Quick Overview

EmailValidator is a PHP library for validating email addresses. It provides robust email validation functionality, adhering to RFC standards and offering various validation modes. The library is designed to be flexible and extensible, allowing developers to customize validation rules as needed.

Pros

  • Comprehensive validation based on RFC standards
  • Multiple validation modes (strict, relaxed, DNS checks)
  • Extensible architecture for custom validation rules
  • Well-maintained and actively developed

Cons

  • May be overkill for simple email validation needs
  • Requires PHP 7.2 or higher
  • Some users report occasional false positives or negatives
  • Learning curve for advanced customization

Code Examples

Validating an email address:

use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Validation\RFCValidation;

$validator = new EmailValidator();
$email = 'example@example.com';

if ($validator->isValid($email, new RFCValidation())) {
    echo "The email is valid";
} else {
    echo "The email is not valid";
}

Using multiple validations:

use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Validation\MultipleValidationWithAnd;
use Egulias\EmailValidator\Validation\RFCValidation;
use Egulias\EmailValidator\Validation\DNSCheckValidation;

$validator = new EmailValidator();
$multipleValidations = new MultipleValidationWithAnd([
    new RFCValidation(),
    new DNSCheckValidation()
]);

if ($validator->isValid("example@example.com", $multipleValidations)) {
    echo "The email is valid and the domain has valid MX records";
}

Retrieving error messages:

use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Validation\RFCValidation;

$validator = new EmailValidator();
$email = 'invalid-email';

if (!$validator->isValid($email, new RFCValidation())) {
    $error = $validator->getError();
    echo "Validation error: " . $error->getMessage();
}

Getting Started

To use EmailValidator in your PHP project, follow these steps:

  1. Install the library using Composer:

    composer require egulias/email-validator
    
  2. Include the Composer autoloader in your PHP script:

    require 'vendor/autoload.php';
    
  3. Use the EmailValidator class in your code:

    use Egulias\EmailValidator\EmailValidator;
    use Egulias\EmailValidator\Validation\RFCValidation;
    
    $validator = new EmailValidator();
    $email = 'user@example.com';
    
    if ($validator->isValid($email, new RFCValidation())) {
        echo "The email is valid";
    } else {
        echo "The email is not valid";
    }
    

Competitor Comparisons

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)

Pros of laravel-mongodb

  • Provides MongoDB integration for Laravel, offering a wider range of database functionality
  • Allows for seamless use of MongoDB within the Laravel ecosystem
  • Supports complex data structures and document-based storage

Cons of laravel-mongodb

  • More complex setup and configuration compared to EmailValidator
  • Requires additional knowledge of MongoDB and NoSQL concepts
  • May have a steeper learning curve for developers new to MongoDB

Code Comparison

EmailValidator:

use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Validation\RFCValidation;

$validator = new EmailValidator();
$isValid = $validator->isValid("example@email.com", new RFCValidation());

laravel-mongodb:

use App\Models\User;

$user = User::create([
    'name' => 'John Doe',
    'email' => 'john@example.com',
    'age' => 30
]);

$users = User::where('age', '>', 25)->get();

Summary

EmailValidator is a focused library for validating email addresses, while laravel-mongodb is a comprehensive solution for integrating MongoDB with Laravel. EmailValidator is simpler to use for its specific purpose, while laravel-mongodb offers more extensive database functionality but requires more setup and MongoDB knowledge. The choice between them depends on the project's specific needs and the developer's familiarity with MongoDB and Laravel.

Provides tools to validate values

Pros of Symfony Validator

  • More comprehensive validation framework, covering various data types beyond just emails
  • Integrates seamlessly with the Symfony ecosystem and other Symfony components
  • Offers extensive customization options and constraint annotations

Cons of Symfony Validator

  • Larger footprint and potentially more complex setup for simple email validation tasks
  • May introduce unnecessary dependencies for projects only needing email validation

Code Comparison

EmailValidator:

use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Validation\RFCValidation;

$validator = new EmailValidator();
$isValid = $validator->isValid("example@email.com", new RFCValidation());

Symfony Validator:

use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Validation;

$validator = Validation::createValidator();
$violations = $validator->validate("example@email.com", [new Email()]);
$isValid = count($violations) === 0;

Summary

EmailValidator is a focused library for email validation, while Symfony Validator offers a broader validation framework. EmailValidator is more lightweight and specific to email validation, whereas Symfony Validator provides a more extensive set of validation tools but may be overkill for simple email validation tasks. The choice between the two depends on the project's scope and integration requirements within the Symfony ecosystem.

Reduce misspelled email addresses in your web apps.

Pros of Mailcheck

  • Lightweight and easy to integrate into web applications
  • Offers suggestions for common domain typos (e.g., "gmial.com" to "gmail.com")
  • Supports multiple languages and can be easily extended

Cons of Mailcheck

  • Primarily focused on client-side validation, lacking comprehensive server-side validation
  • Does not perform in-depth RFC compliance checks for email addresses
  • Limited to basic email format validation and domain suggestions

Code Comparison

EmailValidator:

use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Validation\RFCValidation;

$validator = new EmailValidator();
$isValid = $validator->isValid("example@email.com", new RFCValidation());

Mailcheck:

Mailcheck.run({
  email: "example@email.com",
  suggested: function(suggestion) {
    console.log("Did you mean " + suggestion.full + "?");
  }
});

Summary

EmailValidator is a more comprehensive, server-side email validation library that focuses on RFC compliance and detailed validation. Mailcheck, on the other hand, is a lightweight, client-side library that primarily offers domain suggestion functionality to improve user experience. While EmailValidator is better suited for strict email validation, Mailcheck excels in providing a user-friendly interface for catching common typos in email addresses.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

EmailValidator

Build Status Quality Badge Test Coverage

A library for validating emails against several RFC.

Supported RFCs

This library aims to support RFCs:

Supported versions

Current major version with full support is v3

VersionReleasedEOLOnly critical bug fixesFull
v4.x2023/01/07-XX
v3.x2020/12/29-X
v2.1.x2016/05/16YES
v1.22013/19/05YES

Requirements

Note: PHP version upgrades will happen to accomodate to the pace of major frameworks. Minor versions bumps will go via minor versions of this library (i.e: PHP7.3 -> v3.x+1). Major versions will go with major versions of the library

Installation

Run the command below to install via Composer

composer require egulias/email-validator

Getting Started

EmailValidator requires you to decide which (or combination of them) validation/s strategy/ies you'd like to follow for each validation.

A basic example with the RFC validation

<?php

use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Validation\RFCValidation;

$validator = new EmailValidator();
$validator->isValid("example@example.com", new RFCValidation()); //true

Available validations

  1. RFCValidation: Standard RFC-like email validation.
  2. NoRFCWarningsValidation: RFC-like validation that will fail when warnings* are found.
  3. DNSCheckValidation: Will check if there are DNS records that signal that the server accepts emails. This does not entail that the email exists.
  4. MultipleValidationWithAnd: It is a validation that operates over other validations performing a logical and (&&) over the result of each validation.
  5. MessageIDValidation: Follows RFC2822 for message-id to validate that field, that has some differences in the domain part.
  6. Your own validation: You can extend the library behaviour by implementing your own validations.

*warnings: Warnings are deviations from the RFC that in a broader interpretation are accepted.

<?php

use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Validation\DNSCheckValidation;
use Egulias\EmailValidator\Validation\MultipleValidationWithAnd;
use Egulias\EmailValidator\Validation\RFCValidation;

$validator = new EmailValidator();
$multipleValidations = new MultipleValidationWithAnd([
    new RFCValidation(),
    new DNSCheckValidation()
]);
//ietf.org has MX records signaling a server with email capabilities
$validator->isValid("example@ietf.org", $multipleValidations); //true

Additional validations

Validations not present in the RFCs

  1. SpoofCheckValidation: Will check for multi-utf-8 chars that can signal an erroneous email name.

How to extend

It's easy! You just need to implement EmailValidation and you can use your own validation.

Contributing

Please follow the Contribution guide. Is short and simple and will help a lot.

Other Contributors

(You can find current contributors here)

As this is a port from another library and work, here are other people related to the previous one:

  • Ricard Clau @ricardclau: Performance against PHP built-in filter_var (v2 and earlier)
  • Josepf Bielawski @stloyd: For its first re-work of Dominic's lib
  • Dominic Sayers @dominicsayers: The original isemail function

License

Released under the MIT License attached with this code.