How can PHP be used to handle form submissions and send emails with user input data?

To handle form submissions and send emails with user input data in PHP, you can use the $_POST superglobal to retrieve the form data, validate it, and then use the mail() function to send an email with the user input data.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    
    // Validate input data here
    
    $to = "recipient@example.com";
    $subject = "New message from $name";
    $body = "From: $name\nEmail: $email\nMessage: $message";
    
    if (mail($to, $subject, $body)) {
        echo "Email sent successfully!";
    } else {
        echo "Failed to send email.";
    }
}
?>