What are some common pitfalls when using PHP mail() function for sending emails?

One common pitfall when using the PHP mail() function is not properly setting the headers, which can result in emails being marked as spam. To solve this issue, make sure to include necessary headers such as From, Reply-To, and Content-Type in the email headers.

$to = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email.";
$headers = "From: sender@example.com\r\n";
$headers .= "Reply-To: sender@example.com\r\n";
$headers .= "Content-type: text/html\r\n";

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