The methods for retrieving the form ID and submitted fields have changed in this plugin since 2015, and again in 2020 since writing this answer.
To get the form ID, you should use this:
$form_id = $contact_form->id();
To get the submission data you should use this (instead of $_POST). The function get_posted_data()
returns a string (if you supplied a key to pull specifically) or an array of string values (if no parameters are sent and you want everything).
$posted_data = $submission->get_posted_data();
To put it all together, your snippet would look like this:
add_action( 'wpcf7_before_send_mail', 'wpcf7_add_text_to_mail_body', 10, 3 );
function wpcf7_add_text_to_mail_body( $contact_form, $abort, $submission ) {
//Get the form ID
$form_id = $contact_form->id();
//Do something specifically for form with the ID "123"
if( $form_id == 123 ) {
$posted_data = $submission->get_posted_data();
$values_list = $posted_data['valsitems'];
$values_str = implode(", ", $values_list);
// get mail property
$mail = $contact_form->prop( 'mail' ); // returns array
// add content to email body
$mail['body'] .= 'INDUSTRIES SELECTED';
$mail['body'] .= $values_list;
// set mail property with changed value(s)
$contact_form->set_properties( array( 'mail' => $mail ) );
}
}
Below is the original snippet I wrote in this answer and can be used for older versions of Contact Form 7 that do not pass the $submission
variable.
add_action( 'wpcf7_before_send_mail', 'wpcf7_add_text_to_mail_body', 10, 1 );
function wpcf7_add_text_to_mail_body( $contact_form ) {
//Get the form ID
$form_id = $contact_form->id();
//Do something specifically for form with the ID "123"
if( $form_id == 123 ) {
$submission = WPCF7_Submission::get_instance();//Get the current form submission
$posted_data = $submission->get_posted_data();
$values_list = $posted_data['valsitems'];
$values_str = implode(", ", $values_list);
// get mail property
$mail = $contact_form->prop( 'mail' ); // returns array
// add content to email body
$mail['body'] .= 'INDUSTRIES SELECTED';
$mail['body'] .= $values_list;
// set mail property with changed value(s)
$contact_form->set_properties( array( 'mail' => $mail ) );
}
}