4

I want to validate the e-mail domain but I don't want to worry about any possible subdomain that may appears.

For example:

@abc.com
@a.abc.com
@b.abc.com
...

These should all be valid.

Also, I have a list of domains to validate, such as abc.com, xyz.com... how is the best way to validate e-mail domains from a list, including subdomains?

Thanks.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
user1893187
  • 189
  • 2
  • 2
  • 8

6 Answers6

5

I decided to rewrite this to be more friendly so that you aren't restricted on what type of domain scheme you whitelist.

$whitelist = array("abc.com", "xyz.com", "specific.subdomain.com", "really.specific.subdomain.com"); //You can add basically whatever you want here because it checks for one of these strings to be at the end of the $email string.
$email = "@d.xyz.com";

function validateEmailDomain($email, $domains) {
    foreach ($domains as $domain) {
        $pos = strpos($email, $domain, strlen($email) - strlen($domain));

        if ($pos === false)
            continue;

        if ($pos == 0 || $email[(int) $pos - 1] == "@" || $email[(int) $pos - 1] == ".")
            return true;
    }

    return false;
}

So, you'd use this like:

if (validateEmailDomain($email, $whitelist))
    //Do something.
crush
  • 16,713
  • 9
  • 59
  • 100
5

You can also validate the domain using dns:

function validEmail($email)
{
    $allowedDomains = array('abc.com');
    list($user, $domain) = explode('@', $email);
    if (checkdnsrr($domain, 'MX') && in_array($domain, $allowedDomains))
    {
        return true;
    }
    return false;
}
Scottymeuk
  • 791
  • 1
  • 6
  • 15
  • You cannot simply explode on `@`. Because an escaped `@` is allowed in the local part. See [RFC 5322](http://tools.ietf.org/html/rfc5322). – PeeHaa Apr 24 '13 at 18:19
2

I wrote this function a while back. It may fill the requirements on what you're looking for. It does two things, validates the email address is a valid address and then validates if the domain name is a valid name against it's MX record in DNS.

function validate_email($email) {
    // Check email syntax
    if(preg_match('/^([a-zA-Z0-9\._\+-]+)\@((\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,7}|[0-9]{1,3})(\]?))$/', $email, $matches)) {
        $user = $matches[1];
        $domain = $matches[2];

        // Check availability of DNS MX records
        if(getmxrr($domain, $mxhosts, $mxweight)) {
            for($i=0;$i<count($mxhosts);$i++){
                $mxs[$mxhosts[$i]] = $mxweight[$i];
            }

            // Sort the records
            asort($mxs);
            $mailers = array_keys($mxs);
        } elseif(checkdnsrr($domain, 'A')) {
            $mailers[0] = gethostbyname($domain);
        } else {
            $mailers = array();
        }
        $total = count($mailers);

        // Added to still catch domains with no MX records
        if($total == 0 || !$total) {
            $error = "No MX record found for the domain.";
        }
    } else {
        $error = "Address syntax not correct.";
    }

    return ($error ? $error : TRUE);
}
Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
1

I wrote a simple example of the regular expression with the ability to check the list of domain.

    <?php

    $email = 'shagtv@a.xyz.com';

    $domains = array('abc.com', 'xyz.com');

    $pattern = "/^[a-z0-9._%+-]+@[a-z0-9.-]*(" . implode('|', $domains) . ")$/i";

    if (preg_match($pattern, $email)) {
        echo 'valid';
    } else {
        echo 'not valid';
    }
    ?>
0

Well you should use some code like this:

<?php
$subject = "abcdef";
$pattern = '/^def/';
preg_match($pattern, substr($subject,3), $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>

or this:

<?php
$email = "someone@exa mple.com";

if(!filter_var($email, FILTER_VALIDATE_EMAIL))
  {
  echo "E-mail is not valid";
  }
else
  {
      echo "E-mail is valid";
  }
?>

or this that you have to do a little modify with this page.

Hope it helps!

Community
  • 1
  • 1
Lexsoul
  • 155
  • 2
  • 8
0

Very similar to this post: PHP Check domain of email being registered is a 'school.edu' address

However, you need to take it one step further. Once you have that split, look at parse_url http://php.net/manual/en/function.parse-url.php and grab the HOST part.

Community
  • 1
  • 1
brixon
  • 19
  • 2