How to add php in html php mail funtion

To add PHP in an HTML file and use the PHP mail function, you'll need to follow these steps:

Step 1: Create a new file with a .php extension

Create a new file with a .php extension, for example, contact.php. This file will contain both HTML and PHP code.

Step 2: Add HTML code

Add the HTML code that you want to use as the template for your contact form. For example:

<!DOCTYPE html>
<html>
<head>
    <title>Contact Form</title>
</head>
<body>
    <h1>Contact Form</h1>
    <form action="contact.php" method="post">
        <label for="name">Name:</label>
        <input type="text" id="name" name="name"><br><br>
        <label for="email">Email:</label>
        <input type="email" id="email" name="email"><br><br>
        <label for="message">Message:</label>
        <textarea id="message" name="message"></textarea><br><br>
        <input type="submit" value="Send">
    </form>
</body>
</html>

Step 3: Add PHP code

Add the PHP code that will handle the form submission and send the email. For example:

<?php
// Check if the form has been submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form data
    $name = $_POST["name"];
    $email = $_POST["email"];
    $message = $_POST["message"];

    // Set the email headers
    $to = "[email protected]";
    $subject = "Contact Form Submission";
    $headers = "From: $email\r\nReply-To: $email\r\n";

    // Send the email
    if (mail($to, $subject, $message, $headers)) {
        echo "Email sent successfully!";
    } else {
        echo "Error sending email!";
    }
}
?>

Step 4: Save the file

Save the contact.php file with both HTML and PHP code.

Step 5: Upload the file to your server

Upload the contact.php file to your server, making sure that it's in a directory that can be accessed by your web server.

Step 6: Test the form

Open the contact.php file in your web browser and test the form by submitting it. If everything is set up correctly, you should receive an email with the form data.

Here's the complete code:

<!DOCTYPE html>
<html>
<head>
    <title>Contact Form</title>
</head>
<body>
    <h1>Contact Form</h1>
    <form action="contact.php" method="post">
        <label for="name">Name:</label>
        <input type="text" id="name" name="name"><br><br>
        <label for="email">Email:</label>
        <input type="email" id="email" name="email"><br><br>
        <label for="message">Message:</label>
        <textarea id="message" name="message"></textarea><br><br>
        <input type="submit" value="Send">
    </form>
</body>
</html>

<?php
// Check if the form has been submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form data
    $name = $_POST["name"];
    $email = $_POST["email"];
    $message = $_POST["message"];

    // Set the email headers
    $to = "[email protected]";
    $subject = "Contact Form Submission";
    $headers = "From: $email\r\nReply-To: $email\r\n";

    // Send the email
    if (mail($to, $subject, $message, $headers)) {
        echo "Email sent successfully!";
    } else {
        echo "Error sending email!";
    }
}
?>

Note: Make sure to replace [email protected] with your actual email address.