Php html email form

Here is an example of a simple PHP script that sends an email using an HTML form:

index.html

<form action="send_email.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>

send_email.php

<?php
// Configuration
$to_email = '[email protected]'; // Replace with your email address
$subject = 'Contact Form Submission';

// 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!';
  exit;
}

// Send the email
$headers = 'From: '. $email. "\r\n".
           'Reply-To: '. $email. "\r\n".
           'MIME-Version: 1.0'. "\r\n".
           'Content-Type: text/html; charset=UTF-8';

$message_body = '<html><body>';
$message_body.= '<h2>Contact Form Submission</h2>';
$message_body.= '<p>Name: '. $name. '</p>';
$message_body.= '<p>Email: '. $email. '</p>';
$message_body.= '<p>Message:</p>';
$message_body.= '<p>'. $message. '</p>';
$message_body.= '</body></html>';

mail($to_email, $subject, $message_body, $headers);

// Send a success message
echo 'Thank you for contacting us!';
?>

Here's how it works:

  1. The HTML form is sent to the send_email.php script when the user submits it.
  2. The script gets the form data using $_POST.
  3. It validates the form data to make sure all fields are filled out.
  4. If the form data is valid, it sends an email using the mail() function.
  5. The email is sent with a subject, from address, and a message body that includes the form data.
  6. The script sends a success message to the user.

Note that this is a very basic example, and you should consider adding more security measures to your script, such as validating the form data more thoroughly and preventing SQL injection attacks. You should also test your script thoroughly to make sure it works as expected.