Welcome Guest, Not a member yet? Register   Sign In
The best way of verifying an email
#1

[eluser]Leggy[/eluser]
Well i have been fiddling with an old function recently and as many of you know checkdnsrr() doesn't work on windows servers so i decided to create an email checking function which checks the servers OS and if it is windows it uses fsockopen on port 25 to check. Here is my current function:

Code:
<?php
function check_email( $email, $rec_type = 'MX' )
{
    // Check if the domain is the correct format e.g. [email protected]
    if( !@preg_match( '/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix', $email ) ) return;
    else
    {
        // Check if they included a rec type to check the server
        if( $rec_type == ( '' or null or false ) ) return true;
        else
        {
            // Seperate the username and domain to for checking
            list( $username, $domain ) = @split( '@', $email );
            
            // Check if the server is a Windows server
            if( @stristr( PHP_OS, 'Win' ) and PHP_OS != 'Darwin' )
            {
                // Check the socket (25)
                if( @fsockopen( $domain, 25, $errno, $errstr, 30 ) ) return true;
                else return;
            }
            else
            {
                // Check the DNS record
                if( @checkdnsrr( $domain, $rec_type ) == true ) return true;
                else return;
            }
        }
    }
}
?>

What would you recommend adding?

I was also considering modifying this so be able to use it in the verification class instead of its current email checker which only uses regex.
#2

[eluser]wolfman2000[/eluser]
The only thing I am unsure of is this line of code:
Code:
if( $rec_type == /* Start Part */( '' or null or false ) /* End Part */ ) return true;

I thought you weren't allowed to check for multiple values like that! Since when was this available?
#3

[eluser]Leggy[/eluser]
Really?! it works with me when i was just using a basic page not in CI. Is CI different?

Also what do you think of the code it's self, and how can i check if the email account exists e.g. [email protected] exists but [email protected] doesn't?
#4

[eluser]stanleyxu[/eluser]
The best way to verify an email address is to send a verification mail to that account. And let its owner confirm it.
#5

[eluser]Jamie Rumbelow[/eluser]
Yes. To confirm email addresses you would have to have an admin account on the server. Even then it's complicated.
#6

[eluser]Sean Murphy[/eluser]
If you're interested, here's the function I use to validate email addresses. I originally got it from http://www.ilovejackdaniels.com/, but I might have made adjustments over the years. It's one of the best I've found.

Code:
/**
* Valid Email
*
* @access    public
* @param    string
* @return    bool
*/    
function valid_email($str) {
    // Check for invalid characters
    if (preg_match('/[\x00-\x1F\x7F-\xFF]/', $str))
        return false;

    // Check that there's one @ symbol, and that the lengths are right
    if (!preg_match('/^[^@]{1,64}@[^@]{1,255}$/', $str))
        return false;

    // Split it into sections to make life easier
    $email_array = explode('@', $str);

    // Check local part
    $local_array = explode('.', $email_array[0]);
    foreach ($local_array as $local_part) {
        if (!preg_match('/^(([A-Za-z0-9!#$%&\'*+\/=?^_`{|}~-]+)|("[^"]+"))$/', $local_part))
            return false;
    }

    // Check domain part
    if (!preg_match('/^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}$/', $email_array[1]) ||
        !preg_match('/^\[(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}\]$/', $email_array[1])) {
        // If not an IP address
        
        $domain_array = explode('.', $email_array[1]);
        if (sizeof($domain_array) < 2)
            return false; // Not enough parts to be a valid domain

        foreach ($domain_array as $domain_part) {
            if (!preg_match('/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]))$/', $domain_part))
                return false;
        }
    }
    
    return true;
}
#7

[eluser]Leggy[/eluser]
@stanleyxu - Yes, true but for a general use thing like a comment system sending the email would just get annoying

@Jemgames - I did see a function where is gets a response from the server to weather the email is valid and it could tell when i gave it an invalid email e.g. [email protected]

@Sean Murphy - I have seen that too, from what i see it's just a bigger and more complicated regex thing to see if the email is the right format, not its validity. His regex is probably better and more in depth then the one i used.

Here is both of the function's together: (I have been having a problem with ip's though e.g. I tested with my friends server's ip and it doesn't work (either fsockopen or checkdnsrr))

Code:
function check_email( $email, $rec_type = 'MX' )
{
    // Check that there is one @ symbol, and that the lengths are right
    if( !ereg( '^[^@]{1,64}@[^@]{1,255}$', $email ) ) return;
    
    // Split it into sections to make life easier
    list( $user, $domain ) = @split( '@', $email );
    
    $local_array = explode( '.', $user );
    
    // Check the beggining of the email (before the @) to see if it is valid
    for( $i = 0; $i < sizeof( $local_array ); $i++ )
    {
        if( !ereg( '^(([A-Za-z0-9!#$%&\'*+/=?^_`{|}~-][A-Za-z0-9!#$%&\'*+/=?^_`{|}~\.-]{0,63})|("[^(\\|\")]{0,62}"))$', $local_array[$i] ) ) return;
    }
    
    // Check if domain is IP. If not, it should be valid domain name
    if( !ereg( '^\[?[0-9\.]+\]?$', $domain ) )
    {
        $domain_array = explode( '.', $domain );
        
        // Check if the domain contains domain.*
        if( sizeof( $domain_array ) < 2 ) return;
        
        for( $i = 0; $i < sizeof( $domain_array ); $i++ )
        {
            if( !ereg( '^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$', $domain_array[$i] ) ) return;
        }
    }
    
    // Check if they included a rec type to check the server
    if( $rec_type == ( '' or null or false ) ) return true;
    else
    {
        // Check if the server is a Windows server
        if( @stristr( PHP_OS, 'Win' ) and PHP_OS != 'Darwin' )
        {
            // Check the domain's socket (25)
            if( @fsockopen( $domain, 25, $errno, $errstr, 30 ) ) return true;
            else return;
        }
        else
        {
            // Check the DNS record
            if( @checkdnsrr( $domain, $rec_type ) == true ) return true;
            else return;
        }
    }
}
#8

[eluser]stanleyxu[/eluser]
Okay, your function is good enough. I think, a DNS check is too much and too strict... (I am not very sure, if a slow DNS check would block script executions of other requests.)

Actually, for anti-spam propose, you can simply check the syntax of email and let user enter some security code (Captcha) or questions.

If (valid) fake mail addresses are not welcome, you should disable comment for guest.
#9

[eluser]Leggy[/eluser]
Yeah, i just want a good email checker, i am a perfectionist Wink

And i agree with the dns check, i will make a few changes with the regex to make it check for IP's a bit better. But and dns check is unnecessary.




Theme © iAndrew 2016 - Forum software by © MyBB