What is the purpose of using PHP to send form data in an HTML email?

When sending form data in an HTML email, using PHP can help process the form data and format it correctly before sending it via email. PHP can be used to retrieve the form data, sanitize it to prevent security vulnerabilities, and then format it in HTML to be included in the email body.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $name = $_POST['name'];
  $email = $_POST['email'];
  $message = $_POST['message'];
  
  $to = 'recipient@example.com';
  $subject = 'Contact Form Submission';
  
  $headers = "MIME-Version: 1.0" . "\r\n";
  $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
  $headers .= 'From: ' . $email . "\r\n";
  
  $body = "<p>Name: $name</p>";
  $body .= "<p>Email: $email</p>";
  $body .= "<p>Message: $message</p>";
  
  mail($to, $subject, $body, $headers);
}
?>