Avatar billede blood Nybegynder
11. januar 2001 - 16:36 Der er 13 kommentarer og
1 løsning

Tjek email

Hvordan laver jeg en kode som tjekker om den skrevet email er gyltig?
Den skal bede tjekke @ og .

Hvordan gør jeg det?
Avatar billede halnet Nybegynder
11. januar 2001 - 16:39 #1
Det gør du med regular expressions... ;o)

- Anders
Avatar billede razor Nybegynder
11. januar 2001 - 16:40 #2
function check_email ($q) {
  list($user,$host) = explode(\"@\",$q);
  if (!$q) return false;
  elseif (!ereg(\"@\",$q)) return false;
  elseif (!$user) return false;
  elseif (eregi(\"[^a-z0-9.-_]\",$user)) return false;
  elseif (!checkdnsrr($host,\"ANY\")) return false;
  else return true;
}
Avatar billede halnet Nybegynder
11. januar 2001 - 16:40 #3
Avatar billede iss Novice
11. januar 2001 - 16:40 #4
Den simple:
$regex = \'^([._a-z0-9-]+[._a-z0-9-]*)@(([a-z0-9-]+\\.)*([a-z0-9-]+)(\\.[a-z]{2,3}))$\';
if (!eregi($regex,$email) {...} else {...}

Har en der spørger mail serveren om postkassen eksistere, den kan jeg godt e-maile dig...
Avatar billede emileej Nybegynder
11. januar 2001 - 16:45 #5
>>iss

Jeg kunne også godt tænke mig det script 0:-) =

E-)mil

johansen_emil@hotmail.com
Avatar billede codebase Praktikant
11. januar 2001 - 16:46 #6
meee too :)
janus@mailme.dk
Avatar billede blood Nybegynder
11. januar 2001 - 16:47 #7
>>iss
simon_holm@mail.tele.dk

Prøv at send den
Avatar billede halnet Nybegynder
11. januar 2001 - 16:48 #8
I kommer også langt med razor\'s checkdnsrr()...

- Anders
Avatar billede limemedia Nybegynder
11. januar 2001 - 16:48 #9
iss >> kunne du ikke poste det her ?
Avatar billede iss Novice
11. januar 2001 - 16:49 #10
ljweb -> Det bliver jeg vist nød til.... Jeg tror ikke at CC feltet er stort nok:)))

Giv mig lige 5 min til at finde det...
Avatar billede razor Nybegynder
11. januar 2001 - 16:52 #11
<?php

/*
Originally 
By: Jon S. Stevens jon@clearink.com
Copyright 1998 Jon S. Stevens, Clear Ink
This code has all the normal disclaimers.
It is free for any use, just keep the credits intact.

Enacements and modifications:

          By:  Shane Y. Gibson  shane@tuna.org
Organization:  The Unix Network Archives (http://www.tuna.org./)
        Date:  November 16th, 1998
      Changes:  Added **all** comments, as original code lacked them.
                Added some return codes to include a bit more description
                for useability.

I disclaim nothing...nor do I claim anything...but
it would be nice if you included this disclaimer...

*/

/*  This function takes in an email address (say \'shane@tuna.org\')
*  and tests to see if it\'s a valid email address.
*
*  An array with the results is passed back to the caller.
*
*  Possible result codes for the array items are:
*
*  Item 0:  [true|false]        true for valid email address
*                    false for NON-valid email address
*
*  Item 1:  [SMTP Code]        if a valid MX mail server found, then
*                    fill this array in with failed SMTP
*                    reply codes
*
*  Item 2:  [true|false]        true for valid mail server found for
*                    host/domain
*                    false if no valid mail server found
*
*  Item 3:  [MX server]        if a valid MX host was found and
*                    connected to then fill in this item 
*                    with the MX server hostname
*
*  EXAMPLE code for use is at the very end of this function.
*/

function validateEmail ( $email )
{
    // used for SMTP HELO argument
    global $SERVER_NAME; 

    // initialize our return array, populating with default values
    $return = array ( false,  \"\",  \"\",  \"\" );

    // assign our user part and domain parts respectively to seperate            // variables
    list ( $user, $domain )  = split (  \"@\", $email, 2 );
   
    // split up the domain name into sub-parts
    $arr = explode (  \".\", $domain );

    // figure out how many parts to the host/domain name portion there are
    $count = count ( $arr );

    // get our Top-Level Domain portion (i.e. foobar.org)
    $tld = $arr[$count - 2] .  \".\" . $arr[$count - 1];

    // check that an MX record exists for Top-Level Domain, and if so
    // start our email address checking
    if ( checkdnsrr ( $tld,  \"MX\" ) )
    {
        // Okay...valid dns reverse record; test that MX record for 
        // host exists, and then fill the \'mxhosts\' and \'weight\'
        // arrays with the correct information
        //
        if ( getmxrr ( $tld, $mxhosts, $weight ) )
        {
            // sift through the \'mxhosts\' connecting to each host
            for ( $i = 0; $i < count ( $mxhosts ); $i++ )
            {
                // open socket on port 25 to mxhosts, setting                        // returned file pointer to the variable \'fp\'
                $fp = fsockopen ( $mxhosts[$i], 25 );

                // if the \'fp\' was set, then goto work
                if ( $fp )
                {
                    // work variables
                    $s = 0;
                    $c = 0;
                    $out =  \"\";
       
                    // set our created socket for \'fp\' to     
                    // non-blocking mode
                    // so our fgets() calls will return
                    // right away
                    set_socket_blocking ( $fp, false );

                    // as long as our \'out\' variable has a
                    // null value (\"\")
                    // keep looping (do) until we get
                    // something
                    //
                    do
                    {
                        // output of the stream assigned
                        // to \'out\' variable
                        $out = fgets ( $fp, 2500 );

                        // if we get an \"220\" code (service ready code (i.e greeting))
                        // increment our work (code (c)) variable, and null
                        // out our output variable for a later loop test
                        //
                        if ( ereg (  \"^220\", $out ) )
                        {
                            $s = 0;
                            $out =  \"\";
                            $c++;
                            $return[2] = true;
                            $return[3] = $mxhosts[$i];
                        }
                        // elseif c is greater than 0
                        // and \'out\' is null (\"\"), 
                        // we got a code back from some
                        // server, and we\'ve passed
                        // through this loop at least
                        // once
                        //
                        else if ( ( $c > 0 ) && ( $out ==  \"\" ) )
                        { 
                            $return[2] = true;
                            break; 
                        }

                        // else increment our \'s\'
                        // counter
                        else
                        { $s++;    }
                   
                        // and if \'s\' is 9999, break, to
                        // keep from looping
                        // infinetly
                        if ( $s == 9999 ) { break; }
                   
                    } while ( $out ==  \"\" );

                    // reset our file pointer to blocking
                    // mode, so we wait
                    // for communication to finish before 
                    // moving on...
                    set_socket_blocking ( $fp, true );

                    // talk to the MX mail server,
                    // validating ourself (HELO)
                    fputs ( $fp,  \"HELO $SERVER_NAME\\n\" );

                    // get the mail servers reply, assign to
                    // \'output\' (ignored)
                    $output = fgets ( $fp, 2000 );

                    // give a bogus \"MAIL FROM:\" header to
                    // the server
                    fputs ( $fp,  \"MAIL FROM: <info@\" . $tld .  \">\\n\" );

                    // get output again (ignored)
                    $output = fgets ( $fp, 2000 );

                    // give RCPT TO: header for the email
                    // address we are testing
                    fputs ( $fp,  \"RCPT TO: <$email>\\n\" );               

                    // get final output for validity testing
                    // (used)

                    $output = fgets ( $fp, 2000 );

                    // test the reply code from the mail
                    // server for the 250 (okay) code
                    if ( ereg (  \"^250\", $output ) )
                    {
                        // set our true/false(ness)
                        // array item for testing
                        $return[0] = true;
                    }
                    else
                    {
                        // otherwise, bogus address,
                        // fillin the 2nd array item
                        // with the mail servers reply
                        // code for user to test if they
                        // want
                        $return[0] = false;
                        $return[1] = $output;
                    }
               
                    // tell the mail server we are done
                    // talking to it
                    fputs ( $fp,  \"QUIT\\n\" );

                    // close the file pointer
                    fclose( $fp );

                    // if we got a good value break,
                    // otherwise, we\'ll keep
                    // trying MX records until we get a good
                    // value, or we
                    // exhaust our possible MX servers
                    if ( $return[0] == true )
                    { break; }
                }
            }
        }
    } else {
    // No MX record appears for the specified Top-Level Domain; possibly
    // an invalid host/domain name was specified.
        $return[0] = false;
        $return[1] =  \"Invalid email address (bad domain name)\";
        $return[2] = false;
    }  // end if checkdnsrr()

    // return the array for the user to test against
    return $return;
}
?>
Avatar billede iss Novice
11. januar 2001 - 16:53 #12
Det er nok det Razor bruger en del af - here goes:
<?php

/*
Originally 
By: Jon S. Stevens jon@clearink.com
Copyright 1998 Jon S. Stevens, Clear Ink
This code has all the normal disclaimers.
It is free for any use, just keep the credits intact.

Enacements and modifications:

          By:  Shane Y. Gibson  shane@tuna.org
Organization:  The Unix Network Archives (http://www.tuna.org./)
        Date:  November 16th, 1998
      Changes:  Added **all** comments, as original code lacked them.
                Added some return codes to include a bit more description
                for useability.

I disclaim nothing...nor do I claim anything...but
it would be nice if you included this disclaimer...

*/

/*  This function takes in an email address (say \'shane@tuna.org\')
*  and tests to see if it\'s a valid email address.
*
*  An array with the results is passed back to the caller.
*
*  Possible result codes for the array items are:
*
*  Item 0:  [true|false]        true for valid email address
*                    false for NON-valid email address
*
*  Item 1:  [SMTP Code]        if a valid MX mail server found, then
*                    fill this array in with failed SMTP
*                    reply codes
*
*  Item 2:  [true|false]        true for valid mail server found for
*                    host/domain
*                    false if no valid mail server found
*
*  Item 3:  [MX server]        if a valid MX host was found and
*                    connected to then fill in this item 
*                    with the MX server hostname
*
*  EXAMPLE code for use is at the very end of this function.
*/

function validateEmail ( $email )
{
    // used for SMTP HELO argument
    global $SERVER_NAME; 

    // initialize our return array, populating with default values
    $return = array ( false,  \"\",  \"\",  \"\" );

    // assign our user part and domain parts respectively to seperate            // variables
    list ( $user, $domain )  = split (  \"@\", $email, 2 );
   
    // split up the domain name into sub-parts
    $arr = explode (  \".\", $domain );

    // figure out how many parts to the host/domain name portion there are
    $count = count ( $arr );

    // get our Top-Level Domain portion (i.e. foobar.org)
    $tld = $arr[$count - 2] .  \".\" . $arr[$count - 1];

    // check that an MX record exists for Top-Level Domain, and if so
    // start our email address checking
    if ( checkdnsrr ( $tld,  \"MX\" ) )
    {
        // Okay...valid dns reverse record; test that MX record for 
        // host exists, and then fill the \'mxhosts\' and \'weight\'
        // arrays with the correct information
        //
        if ( getmxrr ( $tld, $mxhosts, $weight ) )
        {
            // sift through the \'mxhosts\' connecting to each host
            for ( $i = 0; $i < count ( $mxhosts ); $i++ )
            {
                // open socket on port 25 to mxhosts, setting                        // returned file pointer to the variable \'fp\'
                $fp = fsockopen ( $mxhosts[$i], 25 );

                // if the \'fp\' was set, then goto work
                if ( $fp )
                {
                    // work variables
                    $s = 0;
                    $c = 0;
                    $out =  \"\";
       
                    // set our created socket for \'fp\' to     
                    // non-blocking mode
                    // so our fgets() calls will return
                    // right away
                    set_socket_blocking ( $fp, false );

                    // as long as our \'out\' variable has a
                    // null value (\"\")
                    // keep looping (do) until we get
                    // something
                    //
                    do
                    {
                        // output of the stream assigned
                        // to \'out\' variable
                        $out = fgets ( $fp, 2500 );

                        // if we get an \"220\" code (service ready code (i.e greeting))
                        // increment our work (code (c)) variable, and null
                        // out our output variable for a later loop test
                        //
                        if ( ereg (  \"^220\", $out ) )
                        {
                            $s = 0;
                            $out =  \"\";
                            $c++;
                            $return[2] = true;
                            $return[3] = $mxhosts[$i];
                        }
                        // elseif c is greater than 0
                        // and \'out\' is null (\"\"), 
                        // we got a code back from some
                        // server, and we\'ve passed
                        // through this loop at least
                        // once
                        //
                        else if ( ( $c > 0 ) && ( $out ==  \"\" ) )
                        { 
                            $return[2] = true;
                            break; 
                        }

                        // else increment our \'s\'
                        // counter
                        else
                        { $s++;    }
                   
                        // and if \'s\' is 9999, break, to
                        // keep from looping
                        // infinetly
                        if ( $s == 9999 ) { break; }
                   
                    } while ( $out ==  \"\" );

                    // reset our file pointer to blocking
                    // mode, so we wait
                    // for communication to finish before 
                    // moving on...
                    set_socket_blocking ( $fp, true );

                    // talk to the MX mail server,
                    // validating ourself (HELO)
                    fputs ( $fp,  \"HELO $SERVER_NAME\\n\" );

                    // get the mail servers reply, assign to
                    // \'output\' (ignored)
                    $output = fgets ( $fp, 2000 );

                    // give a bogus \"MAIL FROM:\" header to
                    // the server
                    fputs ( $fp,  \"MAIL FROM: <info@\" . $tld .  \">\\n\" );

                    // get output again (ignored)
                    $output = fgets ( $fp, 2000 );

                    // give RCPT TO: header for the email
                    // address we are testing
                    fputs ( $fp,  \"RCPT TO: <$email>\\n\" );               

                    // get final output for validity testing
                    // (used)

                    $output = fgets ( $fp, 2000 );

                    // test the reply code from the mail
                    // server for the 250 (okay) code
                    if ( ereg (  \"^250\", $output ) )
                    {
                        // set our true/false(ness)
                        // array item for testing
                        $return[0] = true;
                    }
                    else
                    {
                        // otherwise, bogus address,
                        // fillin the 2nd array item
                        // with the mail servers reply
                        // code for user to test if they
                        // want
                        $return[0] = false;
                        $return[1] = $output;
                    }
               
                    // tell the mail server we are done
                    // talking to it
                    fputs ( $fp,  \"QUIT\\n\" );

                    // close the file pointer
                    fclose( $fp );

                    // if we got a good value break,
                    // otherwise, we\'ll keep
                    // trying MX records until we get a good
                    // value, or we
                    // exhaust our possible MX servers
                    if ( $return[0] == true )
                    { break; }
                }
            }
        }
    } else {
    // No MX record appears for the specified Top-Level Domain; possibly
    // an invalid host/domain name was specified.
        $return[0] = false;
        $return[1] =  \"Invalid email address (bad domain name)\";
        $return[2] = false;
    }  // end if checkdnsrr()

    // return the array for the user to test against
    return $return;
}
?>

Jeg ved der er nyere revisoner af dette, jeg bruger selv en nyere men kan ikke lige finde den. Og i den udgave jeg selv bruger vil jeg ikke af med, der er rettet MEGET især så det kun passer hos os.
Avatar billede iss Novice
11. januar 2001 - 16:55 #13
Jep, det var det Razor brugte:)
Avatar billede razor Nybegynder
11. januar 2001 - 16:59 #14
nej. den jeg bruger har jeg selv lavet, men den sidste jeg skrev er vist bare en tidligere udgave af det script du har angivet, jeg havde liggende..
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester

IT-JOB