Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function validateEmail( $email, $domainCheck = true, $verify = true ) {
- // Verifica sintassi con espressione regolare
- 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];
- // Verifica se esiste un record MX
- if ( $domainCheck && function_exists('checkdnsrr') ) {
- // Creo un array con i mailserver disponibili
- if ( getmxrr($domain, $mxhosts, $mxweight) ) {
- for ( $i = 0; $i < count($mxhosts); $i++ ){
- $mxs[$mxhosts[$i]] = $mxweight[$i];
- }
- asort($mxs);
- $mailers = array_keys($mxs);
- } else if( checkdnsrr($domain, 'A') ) {
- $mailers[0] = gethostbyname($domain);
- } else {
- $mailers=array();
- }
- $total = count( $mailers );
- // Interrogo ogni mailserver
- if ( $total > 0 && $verify ) {
- // Verifico se il mailserver accetta posta
- for ( $n = 0; $n < $total; $n++ ) {
- // Impostazioni socket
- $connect_timeout = 2;
- $errno = 0;
- $errstr = 0;
- // Indirizzo email di autenticazione
- $probe_address = 'vostra_email@vostrodominio.it';
- // Provo la connessione socket
- if ( $sock = @fsockopen($mailers[$n], 25, $errno , $errstr, $connect_timeout) ) {
- $response = fgets($sock);
- stream_set_timeout($sock, 45);
- $meta = stream_get_meta_data($sock);
- $cmds = array(
- "HELO vostrodominio.it", // Deve essere impostato correttamente!
- "MAIL FROM: <" . $probe_address . ">",
- "RCPT TO: <" . $email . ">",
- "QUIT",
- );
- // Errore di connessione
- if ( ! $meta['timed_out'] && ! preg_match('/^2\d\d/', $response) ) {
- $error = "Errore: <span>" . $mailers[$n] . "</span> dice <span>" . $response . "</span>\n";
- break;
- }
- foreach ( $cmds as $cmd ) {
- $before = microtime(true);
- fputs($sock, "$cmd\r\n");
- $response = fgets($sock, 4096);
- $t = 1000*(microtime(true)-$before);
- if ( ! $meta['timed_out'] && preg_match('/^5\d\d/', $response) ) {
- $error = "Indirizzo non verificato: <span>" . $mailers[$n] . "</span> dice <span>" . $response . "</span>\n";
- break 2;
- }
- }
- fclose($sock);
- break;
- } else if ( $n == $total-1 ) {
- $error = $domain . ": Nessun mailserver per il dominio specificato può essere contattato";
- }
- }
- } else if ( $total <= 0 ) {
- $error = "Nessun DNS record utilizzabile per il dominio <span>" . $domain . "</span>";
- }
- }
- } else {
- $error = "Errore di sintassi: indirizzo email non corretto";
- }
- return ( isset( $error ) ? $error : "Indirizzo email valido!" );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement