RumbleFrog

Email Validation

Dec 19th, 2015
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.02 KB | None | 0 0
  1. <?php
  2. function verifyEmail($toemail, $fromemail, $getdetails = false){
  3.     $email_arr = explode("@", $toemail);
  4.     $domain = array_slice($email_arr, -1);
  5.     $domain = $domain[0];
  6.     // Trim [ and ] from beginning and end of domain string, respectively
  7.     $domain = ltrim($domain, "[");
  8.     $domain = rtrim($domain, "]");
  9.     if( "IPv6:" == substr($domain, 0, strlen("IPv6:")) ) {
  10.         $domain = substr($domain, strlen("IPv6") + 1);
  11.     }
  12.     $mxhosts = array();
  13.     if( filter_var($domain, FILTER_VALIDATE_IP) )
  14.         $mx_ip = $domain;
  15.     else
  16.         getmxrr($domain, $mxhosts, $mxweight);
  17.     if(!empty($mxhosts) )
  18.         $mx_ip = $mxhosts[array_search(min($mxweight), $mxhosts)];
  19.     else {
  20.         if( filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ) {
  21.             $record_a = dns_get_record($domain, DNS_A);
  22.         }
  23.         elseif( filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ) {
  24.             $record_a = dns_get_record($domain, DNS_AAAA);
  25.         }
  26.         if( !empty($record_a) )
  27.             $mx_ip = $record_a[0]['ip'];
  28.         else {
  29.             $result   = "invalid";
  30.             $details .= "No suitable MX records found.";
  31.             return ( (true == $getdetails) ? array($result, $details) : $result );
  32.         }
  33.     }
  34.    
  35.     $connect = @fsockopen($mx_ip, 25);
  36.     if($connect){
  37.         if(preg_match("/^220/i", $out = fgets($connect, 1024))){
  38.             fputs ($connect , "HELO $mx_ip\r\n");
  39.             $out = fgets ($connect, 1024);
  40.             $details .= $out."\n";
  41.  
  42.             fputs ($connect , "MAIL FROM: <$fromemail>\r\n");
  43.             $from = fgets ($connect, 1024);
  44.             $details .= $from."\n";
  45.             fputs ($connect , "RCPT TO: <$toemail>\r\n");
  46.             $to = fgets ($connect, 1024);
  47.             $details .= $to."\n";
  48.             fputs ($connect , "QUIT");
  49.             fclose($connect);
  50.             if(!preg_match("/^250/i", $from) || !preg_match("/^250/i", $to)){
  51.                 $result = "invalid";
  52.             }
  53.             else{
  54.                 $result = "valid";
  55.             }
  56.         }
  57.     }
  58.     else{
  59.         $result = "invalid";
  60.         $details .= "Could not connect to server";
  61.     }
  62.     if($getdetails){
  63.         return array($result, $details);
  64.     }
  65.     else{
  66.         return $result;
  67.     }
  68. }
  69. ?>
Advertisement
Add Comment
Please, Sign In to add comment