Contact Form 7 submit failed validation

I need Contact Form 7 to send one email when validation has been failed. Then send the normal CF7 email when the form is submitted correctly. Ridiculous I know but clients!

I think I’m relatively close with the following:

Read More
function send_failed_vaildation_email( $data ) {
    $messagesend = 'Name:' . $_POST['your-name']; 
    $messagesend .= 'rnEmail:' .$_POST['email']; 
    $messagesend .= 'rnPhone:' .$_POST['validPhone']; 
    $messagesend .= 'rnRate:' .$_POST['rate']; 
    $messagesend .= 'rnBased:' .$_POST['based']; 
    wp_mail('c******y@gmail.com', 'failed validation mail', $messagesend );
}
add_filter("wpcf7_posted_data", "send_failed_vaildation_email");

However this sends all submissions regardless if they pass or fail the validation.

wpcf7_before_send_mail is no good as it only fires once the submission passes validation.

I’m either looking for a different hook to use instead of wpcf7_posted_data that only fires when validation is failed or an if statement I can place around wp_mail for the same effect.

Thanks in advance

Related posts

Leave a Reply

3 comments

  1. This is how I managed to get it working.

    @rnevius answer was failing because the wpcf7_posted_data filter is applied before the data is validated so nothing is invalid at that time.

    Combining @rnevius’ answer with the wpcf7_submit filter and it works as expected.

    Full code:

    function send_failed_vaildation_email() {
        $submission = WPCF7_Submission::get_instance();
        $invalid_fields = $submission->get_invalid_fields();
    
        $posted_data = $submission->get_posted_data();
    
        if ( !empty( $invalid_fields ) ) {
            $messagesend = 'Name:' . $posted_data['your-name']; 
            $messagesend .= 'rnEmail:' . $posted_data['email']; 
            $messagesend .= 'rnPhone:' . $posted_data['validPhone']; 
            $messagesend .= 'rnRate:'  . $posted_data['rate']; 
            $messagesend .= 'rnBased:' . $posted_data['based']; 
            $messagesend .= count($invalid_fields); 
            wp_mail('c*******y@gmail.com', 'failed validation mail', $messagesend );
        }
    }
    add_filter("wpcf7_submit", "send_failed_vaildation_email");
    
  2. This hasn’t been tested, but I’m pretty sure you’re going to need to hook into the get_invalid_fields() instance. Something like:

    function send_failed_vaildation_email() {
        $submission = WPCF7_Submission::get_instance();
        $invalid_fields = $submission->get_invalid_fields();
    
        $posted_data = $submission->get_posted_data();
    
        if ( !empty( $invalid_fields ) ) {
            $messagesend = 'Name:' . $posted_data['your-name']; 
            $messagesend .= 'rnEmail:' . $posted_data['email']; 
            $messagesend .= 'rnPhone:' . $posted_data['validPhone']; 
            $messagesend .= 'rnRate:'  . $posted_data['rate']; 
            $messagesend .= 'rnBased:' . $posted_data['based']; 
            wp_mail('c******y@gmail.com', 'failed validation mail', $messagesend );
        }
    }
    add_filter("wpcf7_posted_data", "send_failed_vaildation_email");
    

    You can see the form submission process in the plugin trac.

    EDIT: I also just noticed the 'wpcf7_validation_error' hook in contact-form.php…That may be all you need, as it only fires when there’s an error.

  3. UPDATED CODE FOR LATEST CONTACT FORM 7

    function is_gmail($email) {  
    if(substr($email, -10) == '@gmail.com') {  
       return true;  
    } else {  
       return false;  
    };  
    };  
    
    function custom_email_validation_filter($result, $tag) {  
    $tag = new WPCF7_Shortcode( $tag );
    if ( 'your-email' == $tag->name ) {
        $the_value = isset( $_POST['your-email'] ) ? trim( $_POST['your-email'] ) : '';
        if(!is_gmail($the_value)){  
                 $result->invalidate( $tag, "Are you sure this is the correct address?" );
       };  
     };  
     return $result;  
    };  
    
    
    add_filter('wpcf7_validate_email','custom_email_validation_filter', 20, 2); // Email field  
    add_filter('wpcf7_validate_email*', 'custom_email_validation_filter', 20, 2); // Required Em