några sidor på nätet om PHP programmering

Sök efter  
Börja här Uppgifter Testa dig själv Tips Sidokarta mm. Länkar Sök

4.2 Email

Nedan finns ett antal funktioner som tillsammans kan verifiera en mailadress genom att fråga av mailservern om adressen är korrekt. Går att använda som sådan, utan att du egentligen behöver förstå hur den funkar. Kul!

(Du kan &auml;ven skriva in ?mail=mail.adress@som.skall.kollas i slutet av urladressen)<br />
<?
        
# detta är bara en testbädd för
        # funktionen som kommer nedan
        
if (! isset($_REQUEST["mail"]) )
        {
                
$testa[] = "anders@enges.org";
                
$testa[] = "nobody_at_all@puv.fi";
                
$testa[] = "nobody@xyz.ass.vdrs.fi";

                foreach(
$testa as $mail)
                {
                        if (
verifyMailAdress($mail, $error ) )
                        {
                                echo
"<b>$mail</b> är OK<br>";
                        }
                        else
                        {
                                echo
"<b>$mail</b> är fel: $error<br>" ;
                        }
                }
        }
        else
        {                
$mail = $_REQUEST["mail"];
                        if (
verifyMailAdress($mail, $error ) )
                        {
                                echo
"<b>$mail</b> är OK<br>";
                        }
                        else
                        {
                                echo
"<b>$mail</b> är fel: $error<br>" ;
                        }
        }

        
# om du tar nedanstående funktioner och sätter in in en
        # fil som du sedan inkluderar så kan du använda
        # verifyMailAdress( <adressen>, <variabel för eventuella felmeddelanden
        # för att kunna *verkligen* validera en mailadress
        
function verifyMailAdress( $adress , &$error )
        {
                
$server = $_SERVER["SERVER_NAME"];
                list(
$user, $domain ) = split( "@", $adress );
                if (
checkdnsrr_compatible( $domain ) )
                {
                        if ( !
getmxrr_compatible( $domain, $mxhost, $mxweight ) )
                        {
                                
$error = "Kunde inte hämta MX datat<br>";
                                return
false;
                        }
                }
                else
                {
                                
$mxhost[] = $domain;
                                
$mxweight[] = 1;
                }
                for(
$i = 0; $i < count( $mxhost ); $i++ )
                {
                        
$weighted_hosts[( $mxweight[$i] )] = $mxhost[$i];
                }
                
ksort( $weighted_hosts );
                foreach(
$weighted_hosts as $host )
                {
                        if ( ! (
$fp = @fsockopen( $host, 25 ) ) )
                        {
                                continue;
                        }
                        
socket_set_blocking( $fp, TRUE );
                        
$stop_time = time() + 10;
                        
$gotResponse = false;
                        
$line = fgets( $fp, 1024 );

                        if (
substr( $line, 0, 3 ) == "220" )
                        {
                                
$stop_time = time() + 10;
                                
$gotResponse = true;
                        }
                        while ( !
$gotResponse )
                        {
                                
sleep(2);
                                
$line = fgets( $fp, 1024 );
                                if (
substr( $line, 0, 3 ) == "220" )
                                {
                                        
$stop_time = time() + 10;
                                        
$gotResponse = true;
                                }
                                elseif (
$line == "" AND ( $gotResponse ))
                                {
                                        break;
                                }
                                elseif (
time() > $stop_time )
                                {
                                        break;
                                }
                        }
                        if ( !
$gotResponse )
                        {
                                continue;
                        }
                        
socket_set_blocking( $fp, TRUE );
                        
fputs( $fp, "HELO $server\r\n" );
                        
fgets( $fp, 1024 );

                        
fputs( $fp, "MAIL FROM: <info@$domain>\r\n" );
                        
fgets( $fp, 1024 );

                        
fputs( $fp, "RCPT TO: <$adress>\r\n" );
                        
$line = fgets( $fp, 1024 );

                        
fputs( $fp, "QUIT\r\n" );
                        
fclose( $fp);
                        if (
substr( $line , 0, 3 ) != "250" )
                        {
                                
$error = $line;
                                return
false;
                        }
                        else
                        {
                                return
true;
                        }
                }
                
$error = "Hittade inte mailservern";
                return
false;
        }

        function
checkdnsrr_compatible( $host, $type = 'MX' )
        {
                if (
strtoupper( substr( PHP_OS, 0, 3 ) == "WIN" ) )
                {
                        
# checkdnserr finns inte i Windows
                        # måste göra en egen...
                        
return checkdnsrr_NT( $host, $type);
                }
                else
                {
                        
# använd den inbyggda
                        
return checkdnsrr( $host, $type );
                }
        }
        function
getmxrr_compatible( $domain, &$mxhost , &$mxweight )
        {
                if (
strtoupper( substr( PHP_OS, 0, 3 ) == "WIN" ) )
                {
                        
# checkdnserr finns inte i Windows
                        # måste göra en egen...
                        
return getmxrr_NT( $domain, $mxhost , $mxweight );
                }
                else
                {
                        
# använd den inbyggda
                        
return getmxrr( $domain, $mxhost , $mxweight );
                }
        }
        function
checkdnsrr_NT( $host, $type = 'MX' )
        {
            if( !empty(
$host ) )
            {
                
# Set Default Type:
                
@exec( "nslookup -type=$type $host", $output );
                while( list(
$k, $line ) = each( $output ) )
                {
                    
# Valid records begin with host name:
                    
if( preg_match( "/^$host/i", $line ) )
                    {
                        
# record found:
                        
return true;
                    }
                }
                return
false;
            }
        }

        function
getmxrr_NT( $hostname, &$mxhosts, &$weight )
        {
                
$match = "/^$hostname\sMX\s";
                
$match .= "preference\s=\s([0-9]+),";
                
$match .= "\s*mail\s*exchanger\s*=\s*(.*)$/i";

            if( !
is_array( $mxhosts ) ) $mxhosts = array();
            if( !empty(
$hostname ) )
            {
                @
exec( "nslookup -type=MX $hostname", $output, $ret );
                foreach(
$output as $line )
                {
                    
# Valid records begin with hostname:
                    
if( preg_match( $match, $line, $parts ) )
                    {
                            
$mxhosts[ $parts[1] ] = $parts[2];
                    }
                }
                if(
count( $mxhosts ) )
                {
                    
reset( $mxhosts );
                    
ksort( $mxhosts );
                    
$i = 0;
                    while( list(
$pref, $host ) = each( $mxhosts ) )
                    {
                        
$mxhosts2[$i] = $host;
                        
$weight[$i] = $pref;
                        
$i++;
                    }
                    
$mxhosts = $mxhosts2;

                    return
true;
                }
                else
                {
                    return
false;
                }
            }
        }

?>
Listning 4.2.1 - samples/mailverify.php
[Visa i separat fönster]

 

   TipsEmail