How can PHP handle form submissions and retrieve input data for use in mail scripts?

To handle form submissions and retrieve input data for use in mail scripts in PHP, you can use the $_POST superglobal array to access form data submitted via the POST method. You can then sanitize and validate the input data before using it in your mail script to prevent security vulnerabilities.

// Handle form submission and retrieve input data
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST["name"];
    $email = $_POST["email"];
    $message = $_POST["message"];

    // Sanitize and validate input data
    $name = filter_var($name, FILTER_SANITIZE_STRING);
    $email = filter_var($email, FILTER_SANITIZE_EMAIL);
    $message = filter_var($message, FILTER_SANITIZE_STRING);

    // Use input data in mail script
    $to = "recipient@example.com";
    $subject = "New message from $name";
    $body = "Name: $name\nEmail: $email\nMessage: $message";
    $headers = "From: $email";

    // Send email
    mail($to, $subject, $body, $headers);
}