The Contact form 7 plugin is a very simple powerful plugin used in many different setups. I mainly use it because of it\’s extensibility. There are many solutions out there for easily add additional functionality to it. One of them is to validate emails according to you own validation mechanism.

Validate disposable emails

For some use cases, you don\’t want people to use fake emails. Especially when it\’s about trying to make sure people can\’t join in some program to get a free ebook or some other marketing material. Therefore you want to prevent those emails from getting in your list.

Find below a quick implementation that can easily be added as a plugin or included in your theme\’s functions.php

<?php


/* contact form 7 */
/* validate disposable emails! */

add_filter( \'wpcf7_validate_email*\', \'custom_email_validation_filter\', 20, 2 );

function custom_email_validation_filter( $result, $tag ) {
    if ( \'n-email\' == $tag->name ) { //TODO change this according to your needs
        $email = isset( $_POST[\'n-email\'] ) ? trim( $_POST[\'n-email\'] ) : \'\'; //TODO change this according to your needs


         $error_msg = \'This email is not allowed\'; // change this according to your needs


        if(!is_disposable_email($email)) :
            $result->invalidate( $tag, $error_msg );
        endif;

    }

    return $result;
}




function is_disposable_email($email){
// Check against list of common disposable email providers & return true if the email provided *doesn\'t* match one of them
    $blocklist = json_decode(
        file_get_contents(
            get_template_directory() . \'/cf7/domains.json\', true
        )
    );

    $parts = explode(\'@\', $email);
    $domain = end($parts);

    if (in_array($domain, $blocklist)) {
        return false;
    } else {
        return true;
    }
}

Here is the domains.json. It includes the list of most used disposable emails. You can ofcourse adjust it to your needs.

Validate business emails

Another use case is that for some b2b landing pages, it\’s vital that the people sign up with their business email address, so that you can a) identify the company\’s domain, b) make sure your message gets in the right inbox! Here is the full plugin.

Therefore I\’ve designed a simple adjustment to the above code. The main difference is actually the source of the data.

/* contact form 7 */
/* validate business emails! */

add_filter( \'wpcf7_validate_email*\', \'custom_business_email_validation_filter\', 20, 2 );

function custom_business_email_validation_filter( $result, $tag ) {
    if ( \'email\' == $tag->name ) {
        $email = isset( $_POST[\'email\'] ) ? trim( $_POST[\'email\'] ) : \'\'; //TODO adjust to your needs

        

       $error_msg = \'Please use a business email\';

      
        if(!is_company_email($email)) :
            $result->invalidate( $tag, $error_msg );
        endif;

    }


    return $result;
}

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
    $blocklist = json_decode(
            file_get_contents(
                    get_template_directory_uri() . \'/domains.json\', true
            )
    );

    $parts = explode(\'@\', $email);
    $domain = end($parts);

    if (in_array($domain, $blocklist)) {
        return false;
    } else {
        return true;
    }
}

Here is the domains.json for this scenario

https://gist.github.com/Hendrikv1990/8d9f5577f71f8771ef7ed0fcbe44c8a4

What else?

What other validations could be relevant? Contact form 7 offers a wide scenario of standard validations, but every business has it its own issues with forms. Sometimes you want to match two email addresses in the same form, and adding a bunch of JS to make sure they are matching isn\’t really making things easier. What other validations could be relevant?

Do I really need more plugins?

Generally, my approach is not to use plugins for everything, because often times you need to be able to customize some part that isn\’t possible in the options of the plugin. Then, having your own plugin, makes it really easy to develop new features. You can read more about my other plugin solutions to discover what is possible.

Leave a Comment

Your email address will not be published.