prefix . 'tt24_forms'; // FIX: Silencing false-positive DB warnings // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching $form_title = $wpdb->get_var( $wpdb->prepare( "SELECT title FROM {$table_forms} WHERE id = %d", $form_id ) ); // phpcs:enable if ( ! $form_title ) { $form_title = esc_html__( 'Unrestricted Form', '24tt-unrestricted-forms' ); } // 2. Determine the Recipient (To) Address // For v1.0, we route to the default WordPress Administrator email. // We apply a filter here so advanced users can override it via their theme's functions.php. $admin_email = get_option( 'admin_email' ); $to_email = apply_filters( 'tt24_forms_notification_email', $admin_email, $form_id ); // 3. Build the Email Subject $site_name = get_bloginfo( 'name' ); /* translators: 1: Site name, 2: Form title */ $subject = sprintf( esc_html__( '[%1$s] New Submission: %2$s', '24tt-unrestricted-forms' ), $site_name, $form_title ); // 4. Construct the Email Body (HTML) // We build a clean, responsive HTML table to display the data professionally. $message = ''; $message .= '
'; $message .= '

' . esc_html( $form_title ) . '

'; $message .= '
'; $message .= '

' . esc_html__( 'You have received a new form submission. Here are the details:', '24tt-unrestricted-forms' ) . '

'; $message .= ''; $message .= ''; // Variables to hunt for a user's email address to set as the Reply-To $reply_to = ''; $ignored_fields = array( 'action', 'tt24_form_id', 'tt24_submit_nonce', '_wp_http_referer' ); foreach ( $post_data as $key => $value ) { // Skip system fields if ( in_array( $key, $ignored_fields, true ) ) { continue; } // Clean up the key for display (e.g., 'first_name' -> 'First Name') $display_key = ucwords( str_replace( array( '_', '-' ), ' ', sanitize_text_field( $key ) ) ); // Handle array values (like multi-select checkboxes) securely if ( is_array( $value ) ) { $display_value = implode( ', ', array_map( 'sanitize_textarea_field', wp_unslash( $value ) ) ); } else { $display_value = sanitize_textarea_field( wp_unslash( $value ) ); } // Add the row to our HTML table. We use nl2br to preserve paragraph line breaks in textareas. $message .= ''; $message .= ''; $message .= ''; $message .= ''; // Hunt for an email address to use for the "Reply-To" header if ( empty( $reply_to ) && is_email( $display_value ) && stripos( $key, 'email' ) !== false ) { $reply_to = sanitize_email( $display_value ); } } $message .= ''; $message .= '
' . esc_html( $display_key ) . '' . nl2br( esc_html( $display_value ) ) . '
'; $message .= '

'; /* translators: %s: Site name */ $message .= sprintf( esc_html__( 'This email was generated automatically by 24TT Unrestricted Forms on %s. You can view all your securely stored data in your WordPress Dashboard.', '24tt-unrestricted-forms' ), $site_name ); $message .= '

'; $message .= '
'; // 5. Configure Email Headers // We strictly set the content type to HTML so our table renders correctly. $headers = array('Content-Type: text/html; charset=UTF-8'); // If we found a valid email address in the submission, set it as the Reply-To if ( ! empty( $reply_to ) ) { $headers[] = 'Reply-To: ' . $reply_to; } // 6. Send the Email via WordPress Core API $mail_sent = wp_mail( $to_email, $subject, $message, $headers ); return $mail_sent; } }