How easy is it to verify that an email address is valid in PHP?

Given an email address, how can I verify that it is valid? (In order for the email domain to accept the email address for this address.)

What does it look like in PHP?

Note. I do not want to verify that the email address itself is syntactically valid. I want to know if the domain will accept email to this address.

I would have thought that there is some way to do this with an MX record or something ...

+3
source share
7 answers

I think your only parameters will be the SMTP RCPT TO or VRFY commands.

RCPT TO , . , (uce prevent, catch-all addresses ..).

VRFY , , , .

PHP, RCPT TO ( ): http://code.google.com/p/php-smtp-email-validation/

+4

.

. , , . , , .

, PHP, ; mail() .

+2

, . API .

script, MX, - , , , . , , .

, , :

http://www.linuxjournal.com/article/9585

, RFC.

/**
Validate an email address.
Provide email address (raw input)
Returns true if the email address has the email 
address format and the domain exists.
*/
function validEmail($email)
{
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $domainLen > 255)
      {
         // domain part length exceeded
         $isValid = false;
      }
      else if ($local[0] == '.' || $local[$localLen-1] == '.')
      {
         // local part starts or ends with '.'
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $local))
      {
         // local part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
      {
         // character not valid in domain part
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $domain))
      {
         // domain part has two consecutive dots
         $isValid = false;
      }
      else if
(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                 str_replace("\\\\","",$local)))
      {
         // character not valid in local part unless 
         // local part is quoted
         if (!preg_match('/^"(\\\\"|[^"])+"$/',
             str_replace("\\\\","",$local)))
         {
            $isValid = false;
         }
      }
      if ($isValid && !(checkdnsrr($domain,"MX") || 
 โ†ชcheckdnsrr($domain,"A")))
      {
         // domain not found in DNS
         $isValid = false;
      }
   }
   return $isValid;
}

, , , .

+1

?

. Dominic Sayers PHP, , . DNS.

. , .

+1

() . , , MX, , SMTP, , , .. . SMTP-, , , DNS-

0

, , PHP getmxrr(), MX .

, ... :

.

Thus, its use is limited to verifying that the domain even has a mail server.

0
source

If you are using PHP 5.2.x, now there is no need to use custom validation functions. PHP comes with a built-in function

-1
source

Source: https://habr.com/ru/post/1745535/


All Articles