How can one ensure that the data in the email appears as entered in the form when using the mail function in PHP?
When using the mail function in PHP to send form data via email, special characters may not display correctly due to encoding issues. To ensure that the data appears as entered in the form, you can use the htmlspecialchars function to encode the data before including it in the email body.
// Get form data
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Encode form data
$name = htmlspecialchars($name);
$email = htmlspecialchars($email);
$message = htmlspecialchars($message);
// Compose email message
$to = "recipient@example.com";
$subject = "Form Submission";
$body = "Name: $name\nEmail: $email\nMessage: $message";
// Send email
mail($to, $subject, $body);