What are common pitfalls when using PHP mail() function for contact forms?

One common pitfall when using the PHP mail() function for contact forms is that emails may be marked as spam by email clients due to incorrect headers or lack of proper formatting. To solve this issue, it is important to set the appropriate headers, such as From, Reply-To, and MIME type, and ensure that the email content is properly formatted.

<?php
$to = "recipient@example.com";
$subject = "Contact Form Submission";
$message = "This is a test email from the contact form.";
$headers = "From: sender@example.com\r\n";
$headers .= "Reply-To: sender@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

mail($to, $subject, $message, $headers);
?>