Jeg har efterhånden tit siddet og bladret rundt på nettet for at finde et ordentligt script til at validere e-mails med. Og eftersom jeg af mystiske årsager aldrig kan finde det script jeg fandt sidst jeg ledte, så er det efterhånden blevet til et par gange 😛
Nu vil jeg så gå så langt som til at sige at jeg har fundet den “perfekte” function til at validere e-mails.
Funktionen gør følgende:
- Tjekker om e-mailen har gyldigt e-mail syntaks
- Laver MX-record tjek på den host der er angivet i e-mailen
- Forsøger at sende en mail fra e-mailen, til e-mailen
Jeg har herunder tilladt mig at vise kildekoden til en redigeret (forkortet) udgave af funktionen som er skrevet af Mark “Tarquin” Wilton-Jones.
/** * Email address validation script * v2.1.1 Written by Mark 'Tarquin' Wilton-Jones - 26-28/01/2004 * Updated 12/02/2004 to allow all valid email address formats and * improve RFC compliance, and use MX record weightings * Updated 09/05/2004 to allow Norwegian characters */ function checkEmail($email, $debug = false){ $msgs = Array(); $msgs[] = 'Received email address: '.$email; if (!preg_match("/^(([^<>()[].,;:s@"]+(.[^<>()[].,;:s@"]+)*)|("([^"r]|([wW]))*"))@(([([0-9]{1,3}.){3}[0-9]{1,3}])|(([a-z-0-9áàäçéèêñóòôöüæøå]+.)+[a-z]{2,}))$/i", $email)){ $msgs[] = 'Email address was not recognised as a valid email pattern'; return $debug ? array(false, $msgs) : false; } $msgs[] = 'Email address was recognised as a valid email pattern'; if (preg_match("/@[[d.]*]$/", $email)){ $mxHost[0] = preg_replace("/[wW]*@[([d.]+)]$/", "$1", $email); $msgs[] = 'Email address contained IP address '.$mxHost[0].' - no need for MX lookup'; } else{ $domain = preg_replace("/^[wW]*@([^@]*)$/i", "$1", $email); if (!getmxrr($domain, $mxHost, $weightings)){ $mxHost[0] = $domain; $msgs[] = 'Failed to obtain MX records, defaulting to '.$domain.' as specified by SMTP protocol'; } else{ array_multisort($weightings, $mxHost); $cnt = ''; $co = 0; foreach ($mxHost as $ch){ $cnt.= ($cnt ? ', ' : '').$ch.' ('.$weightings[$co].')'; $co++; } $msgs[] = 'Obtained the following MX records for '.$domain.': '.$cnt; } } foreach ($mxHost as $currentHost){ $msgs[] = 'Checking MX server: '.$currentHost; if ($connection = fsockopen($currentHost, 25, $error_int, $error_str, 5)){ $msgs[] = 'Created socket ('.$connection.') to '.$currentHost; if (preg_match( "/^2dd/", $cn = fgets($connection, 1024))){ $msgs[] = $currentHost.' sent SMTP connection header - no futher MX servers will be checked: '.$cn; while (preg_match("/^2dd-/", $cn)){ $cn = fgets($connection, 1024); $msgs[] = $currentHost.' sent extra connection header: '.$cn; } if (!$_SERVER){ global $HTTP_SERVER_VARS; $_SERVER = $HTTP_SERVER_VARS; } $localHostIP = gethostbyname(preg_replace("/^.*@|:.*$/", '', $_SERVER['HTTP_HOST'])); $localHostName = gethostbyaddr($localHostIP); fputs($connection, 'HELO '.($localHostName?$localHostName:('['.$localHostIP.']'))."rn"); if ($success = preg_match( "/^2dd/", $hl = fgets($connection, 1024))){ $msgs[] = $currentHost.' sent HELO response: '.$hl; fputs($connection, "MAIL FROM: <$email>rn"); if ($success = preg_match( "/^2dd/", $from = fgets($connection, 1024))){ $msgs[] = $currentHost.' sent MAIL FROM response: '.$from; fputs($connection, "RCPT TO: <$email>rn"); if ($success = preg_match("/^2dd/", $to = fgets($connection, 1024))){ $msgs[] = $currentHost.' sent RCPT TO response: '.$to; } else{ $msgs[] = $currentHost.' rejected recipient: '.$to; } } else{ $msgs[] = $currentHost.' rejected MAIL FROM: '.$from; } } else{ $msgs[] = $currentHost.' rejected HELO: '.$hl; } fputs($connection, "QUITrn"); fgets($connection, 1024); fclose($connection); $msgs[] = $success ? ('Email address was accepted by '.$currentHost) : ('Email address was rejected by '.$currentHost); return $debug ? array($success, $msgs) : $success; } elseif (preg_match("/^550/", $cn)){ $msgs[] = 'Mail domain denies connections from this host - no futher MX servers will be checked: '.$cn; return $debug ? array(false, $msgs) : false; } else{ $msgs[] = $currentHost.' did not send SMTP connection header: '.$cn; } } else{ $msgs[] = 'Failed to create socket to '.$currentHost; } } $msgs[] = 'Could not establish SMTP session with any MX servers'; return $debug ? array(false, $msgs) : false; }}