How to change recipient email on send event in Contact Form 7 (WordPress)

Hello everyone!

Sometimes you may need to send letter from your form on the your website to another recipient, for example user changed some parameters.

You can change recipient email with this hook:

function wpcf7_param_change_on_send($contact_form) {

    $custom_email = $_POST['some_field'];
    if(!empty($custom_email))
    {
      $mail = $contact_form->prop( 'mail' );
      $mail['recipient'] = $custom_email;
      $contact_form->set_properties(array('mail'=>$mail));
    }
}

add_action("wpcf7_before_send_mail", "wpcf7_param_change_on_send");