View Single Post
  #2  
Old 11-23-2011, 08:06 AM
 
exsecror exsecror is offline
 

X-Wizard
  
Join Date: Apr 2007
Posts: 1,284
 

Default Re: Improved Email Address Validation

STEP 3: The creation of func_check_email_towerdata()

Now that you have finished modifying func_check_email() it is time to create the func_check_email_towerdata() function which will use TowerData's Email Validation Service (http://www.towerdata.com/services/web/email_validation.html) to validate the email address further.

NOTE: You must have an account with TowerData for this to work and you must have the SOAP extension and PHP 5.2 or greater. Without any of these conditions met (they are not checked) you will break the entire func_check_email() function which breaks anything dependent on it.

include/func/func.mail.php:
PHP Code:
// {{{ func_check_email_towerdata()
/**
 * This function runs further checks to see if an email 
 * is valid
 * 
 * @param string $email the email address to check
 * @return bool true if valid; false if not
 */
function func_check_email_towerdata($email) {    
    
$default_timeout ini_get('default_socket_timeout');
    list(
$localpart$domain) = split('@'$email);
    
    @
ini_set('default_socket_timeout'3);
        
    try {
        
$client = new SoapClient('http://soap.towerdata.com/validate.wsdl',
                                 array(
'trace'              => 0,
                                       
'connection_timeout' => 3));
                                 
        
$params = array('login'                  => 'YOUR TOWERDATA USERNAME',
                        
'password'               => 'YOUR TOWERDATA PASSWORD',
                        
'version'                => '1.2',
                        
'email_validation_level' => 5,
                        
'correction_emails'      => 'false',
                        
'max_email_corrections'  => 1,
                        
'Records'                => array('Record' => array('email' => $email)));
        
        
$result $client->Validate($params);
        
$code   $result->Records->Record->email->status->code;
        
        @
ini_set('default_socket_timeout'$default_timeout);
        
        switch ((int)
$code) {
            case 
5/* Validation Timeout */
            
case 10/* Syntax OK */
            
case 20/* Syntax OK and domain valid according to the domain database */
            
case 30/* Syntax OK and domain exists */
            
case 40/* Syntax OK, domain exists, and domain can receive email */
            
case 50/* Syntax OK, domain exists, and mailbox does not reject mail */
                
return true;
                break;
            case 
200/* Invalid top-level-domain (TLD) in address */
            
case 310/* Domain does not exist */
            
case 315/* Domain does not have a valid IP address */
            
case 325/* Domain can not receive email */
                
$filter['filter_type']  = 'D';
                
$filter['filter_value'] = $domain;
                
$filter['filter_permanent'] = 0;
                
$filter['filter_expires'] = time() + 2592000/* filter expires after 30 days */

                
@func_array2insert('filter_email_validation'$filtertrue);
                return 
false;
                break;
            case 
400/* The mailbox is invalid or the username does not exist at the domain */
                
$filter['filter_type']  = 'L';
                
$filter['filter_value'] = $localpart;
                
$filter['filter_permanent'] = 0;
                
$filter['filter_expires'] = time() + 1296000/* filter expires after 15 days */

                
@func_array2insert('filter_email_validation'$filtertrue);
                return 
false;
                break;
            case 
420/* Mail is not accepted for this domain */
                
$filter['filter_type']  = 'D';
                
$filter['filter_value'] = $domain;
                
$filter['filter_permanent'] = 0;
                
$filter['filter_expires'] = time() + 2592000/* filter expires after 30 days */
                
                
@func_array2insert('filter_email_validation'$filtertrue);
                return 
false;
                break;
            case 
500/* Addresses with that username are not allowed */
                
$filter['filter_type']  = 'L';
                
$filter['filter_value'] = $localpart;
                
$filter['filter_permanent'] = 0;
                
$filter['filter_expires'] = time() + 1296000/* filter expires after 15 days */

                
@func_array2insert('filter_email_validation'$filtertrue);
                return 
false;
                break;
            case 
505/* Addresses with that domain are not allowed */
                
$filter['filter_type']  = 'D';
                
$filter['filter_value'] = $domain;
                
$filter['filter_permanent'] = 0;
                
$filter['filter_expires'] = time() + 2592000/* filter expires after 30 days */
                
                
@func_array2insert('filter_email_validation'$filtertrue);
                return 
false;
                break;
            default:
                return 
false;
                break;
        }
    }
    catch (
Exception $e)
    {
        @
ini_set('default_socket_timeout'$default_timeout);
        return 
true;
    }
    catch (
SoapFault $e)
    {
        @
ini_set('default_socket_timeout'$default_timeout);
        return 
true;
    }
}
// }}} 

NOTE: By default the system will block failed domain names for 30 days and failed usernames for 15 days. You may tweak these to your desire but you should not go beyond 30 days for sanity reasons.

Once you have fully tested this modification it should help reduce any problems you have from bad e-mails being used that can tie up mail servers or prevent communication. It is suggested you change the language variable txt_email_invalid to be more specific as to why the email address failed when they are attempting to register or use any part of the site that utilizes func_check_email().

Hopefully you will find this as useful as we have over the years.
Reply With Quote