How to hook into Contact Form 7 Before Send

I have a plugin I am writing that I want to interact with Contact Form 7.
In my plugin I added the following action add_action

add_action("wpcf7_before_send_mail", "wpcf7_do_something_else");

function wpcf7_do_something_else(&$wpcf7_data) {

    // Here is the variable where the data are stored!
    var_dump($wpcf7_data);

    // If you want to skip mailing the data, you can do it...
    $wpcf7_data->skip_mail = true;

}

I submitted the contact form but the add_action I had did nothing.
I’m unsure how to make my plugin intercept or do something when Contact Form 7
does something. Any, help on how to do this?

Related posts

Leave a Reply

5 comments

  1. I had to do this to prevent Email from being sent. Hope it helps.

    /*
        Prevent the email sending step for specific form
    */
    add_action("wpcf7_before_send_mail", "wpcf7_do_something_else");  
    function wpcf7_do_something_else($cf7) {
        // get the contact form object
        $wpcf = WPCF7_ContactForm::get_current();
    
        // if you wanna check the ID of the Form $wpcf->id
    
        if (/*Perform check here*/) {
            // If you want to skip mailing the data, you can do it...  
            $wpcf->skip_mail = true;    
        }
    
        return $wpcf;
    }
    

    This code assumes that you are running the latest version of CF7 your code above used to work until a couple months ago when they went and did some refactoring of the code. [Apr 28 ’15]

  2. Since WPCF7 5.2 the wpcf7_before_send_mail hook has changed quite a lot. For reference, here is how to work with this hook in 5.2+

    Skip mail

    function my_skip_mail() {
        return true; // true skips sending the email
    }
    add_filter('wpcf7_skip_mail','my_skip_mail');
    

    Or add skip_mail to the Additional Settings tab on your form in the admin area.

    Getting the Form ID or Post ID

    function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {
    
        $post_id = $submission->get_meta('container_post_id');
        $form_id = $contact_form->id();
    
        // do something       
    
        return $contact_form;
        
    }
    add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );
    

    Get user inputted data

    function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {
    
        $your_name = $submission->get_posted_data('your-field-name');
    
        // do something       
    
        return $contact_form;
        
    }
    add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );
    

    Send the email to a dynamic recipient

    function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {
    
        $dynamic_email = 'email@email.com'; // get your email address...
    
        $properties = $contact_form->get_properties();
        $properties['mail']['recipient'] = $dynamic_email;
        $contact_form->set_properties($properties);
    
        return $contact_form;
        
    }
    add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );
    
  3. I’d like to add that you could just use the wpcf7_skip_mail filter:

    add_filter( 'wpcf7_skip_mail', 'maybe_skip_mail', 10, 2 );
    
    function maybe_skip_mail( $skip_mail, $contact_form ) {
    
        if( /* your condition */ )
            $skip_mail = true;
    
        return $skip_mail;
    
    }
    
  4. You can turn Demo Mode on in additional settings and this will prevent emails from being sent. See below from the CF7 Docs.

    If you set demo_mode: on in the Additional Settings field, the contact
    form will be in the demo mode. In this mode, the contact form will
    skip the process of sending mail and just display “completed
    successfully” as a response message.

  5. If you want to prevent email from being sent, use this code. This code will work if you are using latest version of WPCF7

    function wpcf7_before_send_mail_function( $contact_form, &$abort, $submission ) {
    
      $form_id = $contact_form->id();   
      // Compare the form Id so other form will work as it is.
      if( $form_id == 123 ) {
           // Get field data. For example message field
           $message = $submission->get_posted_data('message');
           $message = trim($message);
           // If messsage field value is empty then set abort to true which will not sent email
           if( empty( $message ) ) {
               $abort = true;
           }
      }
    }
    add_action( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );