This blog post aims to assist you in adding dynamic content to your Contact Form 7 email template and changing the email subject and message content during runtime.
We will follow the steps to resolve the issue.
1. We will use the contact form 7 standard filter “wpcf7_before_send_mail“.
2. Add your custom text to an email template. Make sure that text (Variable) is without any space. See attached screenshot for reference.

3. Modify the email body.
// get mail property $mail = $contact_form->prop( 'mail' ); // returns array // add content to email body $mail['body'] = str_replace( '[delivery_estimate_table]', $delivery_estimate, $mail['body'] ) ;
4. Modify the email body for email template 2. If you are using contact form 7 Mail(2) template. You can use following for it.
// get mail property $mail2 = $contact_form->prop( 'mail_2' ); // returns array // add content to email body $mail2['body'] = str_replace( '[delivery_estimate_table]', $delivery_estimate, $mail2['body'] ) ;
5. Modify the email subject.
$mail['subject'] = str_replace( '[quotation-id]', 'BDC-'.time(), $mail['subject'] );
Full source code.
// add the action
add_action( 'wpcf7_before_send_mail', 'prefix_wpcf7_before_send_mail', 10, 3 );
function prefix_wpcf7_before_send_mail( $contact_form, &$abort, $submission ) {
$form_id = $contact_form->id();
if($form_id == 11 ){
$delivery_estimate = '';
$delivery_estimate .= '
<table border="1" cellpadding="5" cellspacing="0" class="delivery-estimate-table">
<thead>
<tr>
<td>Product Name</td>
<td>Product Quantity</td>
<td>Product Price</td>
<td>Product Total Price</td>
<td>Delivery Postal Code</td>
<td>Delivery /hr Price</td>
<td>Total Delivery Cost</td>
<td>Product & Delivery Total</td>
</tr>
</thead>
<tbody>
<tr>
<td>Testing Product Name</td>
<td>1</td>
<td>$100</td>
<td>$100</td>
<td>234 456</td>
<td>$10/hr</td>
<td>$30</td>
<td>$130</td>
</tr>
</tbody></table>';
$quotation_id = 'Test-12345';
// get mail property
$mail = $contact_form->prop( 'mail' ); // returns array
// add content to email body
$mail['body'] = str_replace( '[delivery_estimate_table]', $delivery_estimate, $mail['body'] ) ;
//$mail['body'] .= $values_str;
$mail['body'] = str_replace( '[quotation-id]', 'BDC-'.time(),$mail['body'] );
$mail['subject'] = str_replace( '[quotation-id]', 'BDC-'.time(), $mail['subject'] );
// get mail property
$mail2 = $contact_form->prop( 'mail_2' ); // returns array
// add content to email body
$mail2['body'] = str_replace( '[delivery_estimate_table]', $delivery_estimate, $mail2['body'] ) ;
//$mail['body'] .= $values_str;
$mail2['body'] = str_replace( '[quotation-id]', 'BDC-'. $quotation_id ,$mail2['body'] );
$mail2['subject'] = str_replace( '[quotation-id]', 'BDC-'. $quotation_id , $mail2['subject'] );
// set mail property with changed value(s)
$contact_form->set_properties( array( 'mail' => $mail, 'mail_2' => $mail2 ) );
return $contact_form;
}
}
Output of the code




