How can PHP developers ensure that form data is correctly displayed in email notifications?

When sending form data in email notifications using PHP, developers should ensure that the data is properly sanitized and formatted to prevent any potential security vulnerabilities or display issues. One way to achieve this is by using the PHP `htmlspecialchars()` function to escape special characters in the form data before including it in the email message.

// Sanitize form data before sending email notification
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);

// Construct email message
$to = 'recipient@example.com';
$subject = 'New form submission';
$body = "Name: $name\n";
$body .= "Email: $email\n";
$body .= "Message: $message";

// Send email notification
mail($to, $subject, $body);