Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.64 KB | None | 0 0
  1. function validateEmail( $email, $domainCheck = true, $verify = true ) {
  2.     // Verifica sintassi con espressione regolare
  3.     if ( preg_match('/^([a-zA-Z0-9\._\+-]+)\@((\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,7}|[0-9]{1,3})(\]?))$/', $email, $matches) ) {
  4.         $user = $matches[1];
  5.         $domain = $matches[2];
  6.  
  7.         // Verifica se esiste un record MX
  8.         if ( $domainCheck && function_exists('checkdnsrr') ) {
  9.         // Creo un array con i mailserver disponibili
  10.             if ( getmxrr($domain, $mxhosts, $mxweight) ) {
  11.                 for ( $i = 0; $i < count($mxhosts); $i++ ){
  12.                     $mxs[$mxhosts[$i]] = $mxweight[$i];
  13.                 }
  14.                 asort($mxs);
  15.                 $mailers = array_keys($mxs);
  16.             } else if( checkdnsrr($domain, 'A') ) {
  17.                 $mailers[0] = gethostbyname($domain);
  18.             } else {
  19.                 $mailers=array();
  20.             }
  21.  
  22.              $total = count( $mailers );
  23.  
  24.              // Interrogo ogni mailserver
  25.             if ( $total > 0 && $verify ) {
  26.                 // Verifico se il mailserver accetta posta
  27.                 for ( $n = 0; $n < $total; $n++ ) {
  28.                     // Impostazioni socket
  29.                     $connect_timeout = 2;
  30.                     $errno = 0;
  31.                     $errstr = 0;
  32.                     // Indirizzo email di autenticazione
  33.                     $probe_address = 'vostra_email@vostrodominio.it';
  34.  
  35.                     // Provo la connessione socket
  36.                     if ( $sock = @fsockopen($mailers[$n], 25, $errno , $errstr, $connect_timeout) ) {
  37.                         $response = fgets($sock);
  38.                         stream_set_timeout($sock, 45);
  39.                         $meta = stream_get_meta_data($sock);
  40.                         $cmds = array(
  41.                                 "HELO vostrodominio.it",  // Deve essere impostato correttamente!
  42.                                 "MAIL FROM: <" . $probe_address . ">",
  43.                                 "RCPT TO: <" . $email . ">",
  44.                                 "QUIT",
  45.                             );
  46.  
  47.                         // Errore di connessione
  48.                         if ( ! $meta['timed_out'] && ! preg_match('/^2\d\d/', $response) ) {
  49.                             $error = "Errore: <span>" . $mailers[$n] . "</span> dice <span>" . $response . "</span>\n";
  50.                             break;
  51.                         }
  52.  
  53.                         foreach ( $cmds as $cmd ) {
  54.                             $before = microtime(true);
  55.                             fputs($sock, "$cmd\r\n");
  56.                             $response = fgets($sock, 4096);
  57.                             $t = 1000*(microtime(true)-$before);
  58.                             if ( ! $meta['timed_out'] && preg_match('/^5\d\d/', $response) ) {
  59.                                 $error = "Indirizzo non verificato: <span>" . $mailers[$n] . "</span> dice <span>" . $response . "</span>\n";
  60.                                 break 2;
  61.                             }
  62.                         }
  63.                         fclose($sock);
  64.                         break;
  65.                     } else if ( $n == $total-1 ) {
  66.                         $error = $domain . ": Nessun mailserver per il dominio specificato può essere contattato";
  67.                     }
  68.                 }
  69.             } else if ( $total <= 0 ) {
  70.                 $error = "Nessun DNS record utilizzabile per il dominio <span>" . $domain . "</span>";
  71.             }
  72.         }
  73.     } else {
  74.         $error = "Errore di sintassi: indirizzo email non corretto";
  75.     }
  76.  
  77.     return ( isset( $error ) ? $error : "Indirizzo email valido!" );
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement