How can variables from a form be integrated into a PHP mailer script for reading and processing?

To integrate variables from a form into a PHP mailer script, you can use the $_POST superglobal to retrieve the form data and then include that data in the email message. You can assign the form variables to new variables for easier handling and then concatenate them within the email message body.

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

    $to = "recipient@example.com";
    $subject = "New message from $name";
    $body = "Name: $name\n";
    $body .= "Email: $email\n";
    $body .= "Message: $message";

    if (mail($to, $subject, $body)) {
        echo "Email sent successfully!";
    } else {
        echo "Email sending failed.";
    }
}
?>