Validate Free Email Providers (Gmail, Yahoo, Hotmail) Domains for Contact Form 7 WordPress Plugin

Validate Free Email Providers (Gmail, Yahoo, Hotmail) Domains for Contact Form 7 WordPress Plugin

Since long time I was looking for solution to validate and block email IDs from free email service providers(gmail, hotmail, yahoo, etc) and only allow company email inputs while submitting form created using contact form 7 plugin in WordPress. It was big need by most of the B2B sites as people post spam messages using fake email IDs. And the number of such submissions is huge.

Maximum number of WordPress portals use contact form 7 for their form processing as because of its flexibility, simplicity and awesome support. I found below codes which eases this task for contact form 7 only.

Method 1:

  1. Open text.php file located at /wp-content/plugin/contact-form-7/modules/text.php
  2. The search for below code
     if ( 'email' == $tag->basetype ) {
           if ( $tag->is_required() && '' == $value ) {
              $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );
            } 
           elseif ( '' != $value && ! wpcf7_is_email( $value ) ) {
            $result->invalidate( $tag, wpcf7_get_message( 'invalid_email' ));
            }       
          }
  3. And replace above code with
    if ( 'email' == $tag->basetype ) {
    
        /*add the domain names you want to block in the $domains array*/
        $domains = array("gmail.com","yahoo.com","yahoo.co.in");
        /*explode will store the string into array
             e.g: example@gmail.com
                    array(example, gmail.com)*/
        $udomain = explode('@', $value);
    
        //select the email domain from the above splitted array
        $email_domain = $udomain[1];
    
        // check name is 'company-email' else default validation will work
        if('company-email' == $tag->name) {
            //check entered value = $value exists in $domain array
            if(in_array($email_domain, $domains)) {
                //display error
                $result->invalidate( $tag, "Please enter your company email address" );
            }
        }
    
        //email field is empty
        if ( $tag->is_required() && '' == $value ) {
            $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );   
        } 
    
        //check basic email validation
       elseif ( '' != $value && ! wpcf7_is_email( $value ) ) {
            $result->invalidate( $tag, wpcf7_get_message( 'invalid_email' ) );
        }
    }

    Please note that in $domains array you can any email service domain you want.

  4. Update text.php file.
  5. Now, to block free email service provider domains use below code
    [email* company-email class:yourclass ] //bock free domains
      [email* your-email class:yourclass ] //default contact form 7 validation
    
     Note: If we use 'company-email', it will block free domains
           If we use 'your-email'/'someothername', default contact  form email validation will work.

Method 2:

Other method is to use below code in function.php and use company-email in place of your-email while building form,

// Add custom validation for CF7 form fields
function is_company_email($email){ // Check against list of common public email providers & return true if the email provided *doesn't* match one of them
	if(
		preg_match('/@gmail.com/i', $email) ||
		preg_match('/@hotmail.com/i', $email) ||
		preg_match('/@live.com/i', $email) ||
		preg_match('/@msn.com/i', $email) ||
		preg_match('/@aol.com/i', $email) ||
		preg_match('/@yahoo.com/i', $email) ||
		preg_match('/@inbox.com/i', $email) ||
		preg_match('/@gmx.com/i', $email) ||
		preg_match('/@me.com/i', $email)
	){
		return false; // It's a publicly available email address
	}else{
		return true; // It's probably a company email address
	}
}

function custom_email_validation_filter($result, $tag) {  

 $tag = new WPCF7_Shortcode( $tag );

   if ( 'company-email' == $tag->name ) {

 $the_value = isset( $_POST['company-email'] ) ? trim( $_POST['company-email'] ) : '';

           if(!is_company_email($the_value)){
                     $result->invalidate( $tag, "Please Enter a valid Business Email ID" );
           }
      }
       return $result;
 }

add_filter( 'wpcf7_validate_email', 'custom_email_validation_filter', 10, 2 );
add_filter( 'wpcf7_validate_email*', 'custom_email_validation_filter', 10, 2 );

2 Comments


Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons
%d bloggers like this: