PHP Classes

File: src/DataTypes/Email.php

Recommend this page to a friend!
  Packages of Amirreza Ebrahimi   HeroQR Powerful PHP QR Code Library to generate PNG, SVG, PDF, Logos Ready to Use with Laravel   src/DataTypes/Email.php   Download  
File: src/DataTypes/Email.php
Role: Class source
Content type: text/plain
Description: Class source
Class: HeroQR Powerful PHP QR Code Library to generate PNG, SVG, PDF, Logos Ready to Use with Laravel
Generate QR code images in several formats
Author: By
Last change: Update of src/DataTypes/Email.php
Date: 6 months ago
Size: 1,304 bytes
 

Contents

Class file image Download
<?php

namespace HeroQR\DataTypes;

use
HeroQR\Contracts\DataTypes\AbstractDataType;

/**
 * Class Email
 *
 * This class provides robust validation for email addresses. It includes:
 * - Validating the email format using PHP's `FILTER_VALIDATE_EMAIL`.
 * - Using a regex pattern to ensure a proper email structure.
 * - Checking for the existence of an MX record for the email domain.
 * - Validating the domain against a predefined blacklist.
 * - Normalizing the domain to handle case-insensitivity.
 *
 * @package HeroQR\DataTypes
 */

class Email extends AbstractDataType
{
    public static function
validate(string $email): bool
   
{
        if (!
filter_var($email, FILTER_VALIDATE_EMAIL)) {
            return
false;
        }

        if (!
preg_match('/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/', $email)) {
            return
false;
        }

       
$domain = substr(strrchr($email, '@'), 1);

       
$blacklist = ['example.com', 'test.com', 'invalid.com', 'nonexistentdomain.xyz'];
        foreach (
$blacklist as $blockedDomain) {
            if (
str_ends_with($domain, $blockedDomain)) {
                return
false;
            }
        }

        if (!
checkdnsrr($domain, 'MX') && !checkdnsrr($domain, 'A')) {
            return
false;
        }

        return
true;
    }
}