What are some best practices for structuring PHP contact form data for easy handling and processing via email?

When structuring PHP contact form data for easy handling and processing via email, it is best practice to organize the data in a structured format such as an associative array. This allows for easy access to the form fields and their values when constructing the email message. Additionally, it is recommended to sanitize and validate the form data before sending it via email to prevent any security vulnerabilities.

// Assuming form data is submitted via POST method
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

// Create an associative array to store the form data
$form_data = array(
    'Name' => $name,
    'Email' => $email,
    'Message' => $message
);

// Construct the email message using the form data
$email_message = "New Contact Form Submission:\n\n";
foreach ($form_data as $key => $value) {
    $email_message .= $key . ": " . $value . "\n";
}

// Send email with the form data
$to = 'recipient@example.com';
$subject = 'New Contact Form Submission';
$headers = 'From: ' . $email;

mail($to, $subject, $email_message, $headers);