This should do the trick:
/**
* Modifies the emails by replacing the conditionals [if field_name][/if]
*
* @param [Object] $cf7 the contanct form 7 object
* @return void
* @author Tobias Braner
**/
public function before_send_mail($contact_form) {
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$posted_data = $submission->get_posted_data();
$mail = $contact_form->prop('mail');
$mail2 = $contact_form->prop('mail2');
$mail_body = $mail['body'];
$mail2_body = $mail2['body'];
$mail['body'] = $this->process_conditions($mail_body, $posted_data);
$mail2['body'] = $this->process_conditions($mail2_body, $posted_data);
$contact_form->set_properties(array('mail' => $mail, 'mail2' => $mail2));
}
}
/**
* Takes the wpcf7 object and replaces all conditions.
*
* @return void
* @author Tobias Braner
**/
private function process_conditions($mail_body, $posted_data) {
$updated_email_body = $mail_body;
$matches = array();
$num_matches = preg_match_all($this->regexp, $mail_body, $matches);
for ($i=0; $i < $num_matches; $i++) {
$expression = $matches[WPCF7MC_EXPRESSION][$i];
$variable = $matches[WPCF7MC_VARIABLE][$i];
$value = $matches[WPCF7MC_VALUE][$i];
if (empty($posted_data[$variable][0]) and array_key_exists($variable, $posted_data)) {
$updated_email_body = str_replace($expression, '', $updated_email_body);
} else {
$updated_email_body = str_replace($expression, $value, $updated_email_body);
}
}
return $updated_email_body;
}