Forums

Validate the mx record using php checkdnsrr

Kannan1976 21 Jan, 2010
Hi there,

Is there a way i can use the checkdnsrr in php to validate the users email address in Chrono Forms? I found this code but can it be integrated with CF onsubmit.


$email = '{Email}';

if(verify_email($email)){

    // E-mail address looks to be in the proper format
    // lets check the MX records

    if(verify_email_dns($email)){

        // E-mail passed both checks
        echo 'Success - E-mail address appears to be valid.';

    } else {

        // E-mail is invalid, no MC record
        echo 'Error - E-mail domain does not have an MX record.';

    }

} else {

    // E-mail inst formatted correctly
    // so we don't even check its MX record
    echo 'Error - E-mail address appears to be invalid.';

}

// Our function to filter our bogus formatted addresses
function verify_email($email){

    if(!preg_match('/^[_A-z0-9-]+((\.|\+)[_A-z0-9-]+)*@[A-z0-9-]+(\.[A-z0-9-]+)*(\.[A-z]{2,4})$/',$email)){
        return false;
    } else {
        return $email;
    }
}

// Our function to verify the MX records
function verify_email_dns($email){

    // This will split the email into its front
    // and back (the domain) portions
    list($name, $domain) = split('@',$email);

    if(!checkdnsrr($domain,'MX')){

        // No MX record found
        return false;

    } else {

        // MX record found, return email
        return $email;

    }
}



If it's possible, where can i place this code?

Thanks.
GreyHead 21 Jan, 2010
Hi Kannan1976,

Looks like the Server-side validation codebox is a good place for this.

You'll need to restructure the code a little so that it meets the validation code requirements and returns a message only if it fails
$email = JRequest::getVar('email', '', 'post');
if ( !$email ) {
  return 'An e-mail is required.';
}
if ( !verify_email($email) ) {
  // E-mail isn't formatted correctly
  return echo 'Error - E-mail address appears to be invalid.';
}

// lets check the MX records
if ( !verify_email_dns($email) ) {
  // E-mail is invalid, no MC record
  return 'Error - E-mail domain does not have an MX record.';
}
// E-mail passed all checks

// Our function to filter our bogus formatted addresses
function verify_email($email)
{
  return preg_match('/^[_A-z0-9-]+((\.|\+)[_A-z0-9-]+)*@[A-z0-9-]+(\.[A-z0-9-]+)*(\.[A-z]{2,4})$/', 
}

// Our function to verify the MX records
function verify_email_dns($email)
{
  // This will split the email into its front
  // and back (the domain) portions
  list($name, $domain) = split('@', $email);

  return checkdnsrr($domain, 'MX');
}
?>
Not checked and may well need debugging

Bob
This topic is locked and no more replies can be posted.