What are the different methods for embedding a form in PHP to collect user data before sending an email, and how can this be integrated into an existing website?

To embed a form in PHP to collect user data before sending an email, you can create an HTML form that submits the data to a PHP script. The PHP script will process the form data and send an email using the mail() function. This can be integrated into an existing website by adding the form code to the desired page and creating a new PHP script to handle the form submission.

<?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\nEmail: $email\nMessage: $message";
    
    if (mail($to, $subject, $body)) {
        echo "Email sent successfully!";
    } else {
        echo "Failed to send email. Please try again.";
    }
}
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <label for="name">Name:</label>
    <input type="text" name="name" required><br>
    
    <label for="email">Email:</label>
    <input type="email" name="email" required><br>
    
    <label for="message">Message:</label>
    <textarea name="message" required></textarea><br>
    
    <input type="submit" value="Send">
</form>