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

Accept only corporate Email ids from your contact form

1
Badge 2 Asked on October 9, 2018 in Wordpress.
Add Comment
1 Answer(s)

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:

    • Open text.php file located at /wp-content/plugin/contact-form-7/modules/text.php

 

    • 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' ));
} 
}
    • 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.

    • Update text.php file.

 

    • 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.
0
Badge 2 Answered on October 9, 2018.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.