Remove Blank Lines in Email for Unfilled Form Items

This quick tip will show you how you can remove blank lines from Contact Form 7 during submission for optional fields which were left empty by the user.

add_filter( 'wpcf7_mail_components', 'remove_blank_lines' );

function remove_blank_lines( $mail ) {
  if ( is_array( $mail ) && ! empty( $mail['body'] ) )
    $mail['body'] = preg_replace( '|\n\s*\n|', "\n\n", $mail['body'] );

  return $mail;

Have a tip to share with us? Please email us here.

Also checkout “Solving Common Contact Form 7 Issues