What are some considerations when integrating PHP scripts with HTML contact form templates?

When integrating PHP scripts with HTML contact form templates, it is important to ensure that the form data is properly validated and sanitized to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. Additionally, the PHP script should handle form submissions, process the data, and send the email to the desired recipient.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    // Validate input fields
    if (empty($name) || empty($email) || empty($message)) {
        echo "All fields are required";
    } else {
        $to = "recipient@example.com";
        $subject = "Contact Form Submission";
        $headers = "From: $email";
        $body = "Name: $name\nEmail: $email\nMessage: $message";

        // Send email
        if (mail($to, $subject, $body, $headers)) {
            echo "Message sent successfully";
        } else {
            echo "Failed to send message";
        }
    }
}
?>