How to make from mail contact form php

Here is a step-by-step guide on how to create a simple contact form using PHP and send the form data via email:

Step 1: Create an HTML Form

Create an HTML file (e.g., contact.html) with the following code:

<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>

This form has three fields: name, email, and message. The action attribute specifies that the form data should be sent to a PHP file called contact.php.

Step 2: Create a PHP File

Create a PHP file (e.g., contact.php) with the following code:

<?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"];

  // Validate the form data
  if (empty($name) || empty($email) || empty($message)) {
    echo "Please fill out all fields.";
  } else {
    // Send the form data via email
    $to = "[email protected]"; // Replace with your email address
    $subject = "Contact Form Submission";
    $body = "Name: $name\nEmail: $email\nMessage: $message";
    $headers = "From: $email\r\nReply-To: $email\r\nContent-Type: text/plain; charset=UTF-8";

    if (mail($to, $subject, $body, $headers)) {
      echo "Thank you for contacting us!";
    } else {
      echo "Error sending email.";
    }
  }
}
?>

This PHP file checks if the form has been submitted using the $_SERVER["REQUEST_METHOD"] variable. If the form has been submitted, it retrieves the form data using the $_POST superglobal array. It then validates the form data by checking if all fields are filled out. If the form data is valid, it sends the form data via email using the mail() function.

Step 3: Configure Your Email Server

Make sure your email server is configured to send emails. You may need to configure your PHP settings to use a specific email server or set up a mail server on your local machine.

Step 4: Test Your Contact Form

Save both files and upload them to your web server. Open the contact.html file in a web browser and fill out the form. Click the "Send" button to submit the form. If everything is set up correctly, you should receive an email with the form data.

That's it! You now have a simple contact form that sends form data via email using PHP.