D
D
davc2018-06-21 19:11:29
PHP
davc, 2018-06-21 19:11:29

How to check if email exists in php?

Hello everyone, Please tell me how you can check the validity of an email (existence) in php

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Aksentiev, 2018-06-21
@davc

https://ctrlq.org/code/20152-validate-email-address
Something like this, but in general you should not do this.
Not all servers generally respond correctly.
All this is done for a very long time. a series of different requests to the DNS and the mailer is needed.
Not all servers respond the first time (hello mail.ru, this is such a protection - if you requested 1 email / sent a letter to it once, then you are a spammer and a hacker and you need to give an error, but if you tried to send again, then you are a normal kid) .
In general, the standard mail validation by letter is not just invented.
And the address itself can be checked for validity simply by the mask {asdasd}@{asdasd}.{asdasd}

V
Vladislav Kadun, 2018-06-21
@ZXZs

/*
*  This script was writed by Setec Astronomy - setec@freemail.it
*
*  This script is distributed under the GPL License
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*   GNU General Public License for more details.
*
*  http://www.gnu.org/licenses/gpl.txt
*
*/
define ('DEBUG_OK', true);
class CCheckMail
{
  var $timeout = 10;
  var $domain_rules = array ("aol.com", "bigfoot.com", "brain.net.pk", "breathemail.net",
                "compuserve.com", "dialnet.co.uk", "glocksoft.com", "home.com",
                "msn.com", "rocketmail.com", "uu.net", "yahoo.com", "yahoo.de");

  function _is_valid_email ($email = "")
    { return preg_match('/^[.\w-][email protected]([\w-]+\.)+[a-zA-Z]{2,6}$/', $email); }

  function _check_domain_rules ($domain = "")
    { return in_array (strtolower ($domain), $this->domain_rules); }

  function execute ($email = ""){
    if (!$this->_is_valid_email ($email)) return false;
    $host = substr (strstr ($email, '@'), 1);

    if ($this->_check_domain_rules ($host)) return false;
    $host .= ".";

    if (getmxrr ($host, $mxhosts[0], $mxhosts[1]) == true)  array_multisort ($mxhosts[1], $mxhosts[0]);
    else { $mxhosts[0] = $host;
       $mxhosts[1] = 10;
     }
    if (DEBUG_OK) print_r ($mxhosts);

    $port = 25;
    $localhost = $_SERVER['HTTP_HOST'];
    $sender = '[email protected]' . $localhost;

    $result = false;
    $id = 0;
    while (!$result && $id < count ($mxhosts[0]))
    { if (function_exists ("fsockopen"))
         { if (DEBUG_OK) print_r ($id . " " . $mxhosts[0][$id]);
           if ($connection = fsockopen ($mxhosts[0][$id], $port, $errno, $error, $this->timeout))
          {
              fputs ($connection,"HELO $localhost\r\n"); // 250
              $data = fgets ($connection,1024);
              $response = substr ($data,0,1);
              if (DEBUG_OK) print_r ($data);
              if ($response == '2') // 200, 250 etc.
             {
                fputs ($connection,"MAIL FROM:<$sender>\r\n");
                $data = fgets($connection,1024);
                $response = substr ($data,0,1);
                if (DEBUG_OK) print_r ($data);
                if ($response == '2') // 200, 250 etc.
               {
                  fputs ($connection,"RCPT TO:<$email>\r\n");
                  $data = fgets($connection,1024);
                  $response = substr ($data,0,1);
                  if (DEBUG_OK) print_r ($data);
              if ($response == '2') // 200, 250 etc.
                 {
                    fputs ($connection,"data\r\n");
                    $data = fgets($connection,1024);
                    $response = substr ($data,0,1);
                    if (DEBUG_OK) print_r ($data);
                    if ($response == '2') // 200, 250 etc.
                   { $result = true; }
                     }
               }
             }
          fputs ($connection,"QUIT\r\n");
              fclose ($connection);
              if ($result) return true;
            }
       }
      else  break;
      $id++;
    } //while
    return false;
}
}

$str='[email protected]'
$alter=new CCheckMail();
print "E-mail: ".$str." - ".($alter->execute($str)?'существует':'не существует');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question