Action wpcf7_before_send_mail

The wpcf7_before_send_mail action hook in WordPress Contact Form 7 (CF7) is triggered just before the form submission is sent via email.

This hook allows you to modify or extend the form submission process before the email is sent. You can use it to perform various tasks, such as:

  1. Validate form data: You can use this hook to validate the form data before sending the email. If the validation fails, you can cancel the email sending process.
  2. Modify form data: You can use this hook to modify the form data before sending the email. For example, you can add or remove fields, or modify the values of existing fields.
  3. Send additional emails: You can use this hook to send additional emails or notifications before sending the main email.
  4. Perform custom actions: You can use this hook to perform custom actions, such as updating a database record or triggering a custom function.

The wpcf7_before_send_mail action hook receives two parameters:

  1. $contact_form: The Contact Form 7 object that is being submitted.
  2. $abort: A boolean value that indicates whether the email sending process should be aborted. If you set $abort to true, the email will not be sent.

Here's an example of how you can use the wpcf7_before_send_mail action hook:

add_action( 'wpcf7_before_send_mail', 'my_before_send_mail_function' );

function my_before_send_mail_function( $contact_form, $abort ) {
    // Get the form data
    $form_data = $contact_form->get_posted_data();

    // Validate the form data
    if (! validate_form_data( $form_data ) ) {
        $abort = true; // Abort the email sending process
    }

    // Modify the form data
    $form_data['custom_field'] = 'Modified value';

    // Send additional emails
    wp_mail( '[email protected]', 'Subject', 'Body' );

    // Perform custom actions
    update_option( 'my_option', 'Value' );
}

In this example, the my_before_send_mail_function function is triggered before the email is sent. It validates the form data, modifies the form data, sends an additional email, and updates a database option. If the form data is invalid, the email sending process is aborted.