+function verifyEmail($toemail, $fromemail, $getdetails = false){
+ $email_arr = explode("@", $toemail);
+ $domain = array_slice($email_arr, -1);
+ // Trim [ and ] from beginning and end of domain string, respectively
+ $domain = ltrim($domain, "[");
+ $domain = rtrim($domain, "]");
+ $result = $details = '';
+ if( "IPv6:" == substr($domain, 0, strlen("IPv6:")) ) {
+ $domain = substr($domain, strlen("IPv6") + 1);
+ if( filter_var($domain, FILTER_VALIDATE_IP) )
+ getmxrr($domain, $mxhosts, $mxweight);
+ $mx_ip = $mxhosts[array_search(min($mxweight), $mxhosts)];
+ if( filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ) {
+ $record_a = dns_get_record($domain, DNS_A);
+ elseif( filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ) {
+ $record_a = dns_get_record($domain, DNS_AAAA);
+ if( !empty($record_a) )
+ $mx_ip = $record_a[0]['ip'];
+ $details .= "No suitable MX records found.";
+ return ( (true == $getdetails) ? array($result, $details) : $result );
+ $connect = @fsockopen($mx_ip, 25);
+ if(preg_match("/^220/i", $out = fgets($connect, 1024))){
+ fputs ($connect , "HELO $mx_ip\r\n");
+ $out = fgets ($connect, 1024);
+ fputs ($connect , "MAIL FROM: <$fromemail>\r\n");
+ $from = fgets ($connect, 1024);
+ $details .= $from."\n";
+ fputs ($connect , "RCPT TO: <$toemail>\r\n");
+ $to = fgets ($connect, 1024);
+ fputs ($connect , "QUIT");
+ if(!preg_match("/^250/i", $from) || !preg_match("/^250/i", $to)){
+ $details .= "Could not connect to server";
+ return array($result, $details);