Here is an example of how you might create a PHP contact form with email validation:
<!-- Contact form HTML --> <form method="post" action="send_email.php"> <label for="name">Name:</label><br> <input type="text" id="name" name="name" required><br> <label for="email">Email:</label><br> <input type="email" id="email" name="email" required><br> <label for="subject">Subject:</label><br> <input type="text" id="subject" name="subject" required><br> <label for="message">Message:</label><br> <textarea id="message" name="message" required></textarea><br> <button type="submit">Send</button> </form> This HTML code defines a simple contact form with four fields: name, email, subject, and message. The required attribute ensures that the user must fill out each field before the form can be submitted. To process the form and send the email, you will need to create a separate PHP script called "send_email.php". Here is an example of how that script might look: <?php // Check that the form has been submitted if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["subject"]) && isset($_POST["message"])) { // Get the form data $name = $_POST["name"]; $email = $_POST["email"]; $subject = $_POST["subject"]; $message = $_POST["message"]; // Validate the email address if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // Set the recipient email address and name $to = "recipient_email@example.com"; $to_name = "Recipient Name"; // Set the sender email address and name $from = $email; $from_name = $name; // Set the email subject and body $email_subject = "Contact Form Submission: $subject"; $email_body = "You have received a new message from the contact form on your website.\n\n" . "Name: $name\n" . "Email: $email\n" . "Subject: $subject\n" . "Message:\n$message\n"; // Set the email headers $headers = "From: $from_name <$from>\r\n"; $headers .= "Reply-To: $from\r\n"; // Send the email mail($to, $email_subject, $email_body, $headers); // Redirect to the thank you page header("Location: thank_you.html"); } else { // The email address is invalid, show an error message echo "Invalid email address"; } } This PHP script first checks that all of the form fields have been submitted. It then gets the form data and uses the 'filter_var' function to validate the email address. If the email address is valid, it constructs an email message using