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

One common pitfall when using the mail() function in PHP is not properly setting the headers, which can lead to emails being marked as spam or not being delivered at all. To solve this, make sure to include headers such as From, Reply-To, and Content-Type in your mail function call.

$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; charset=ISO-8859-1\r\n";

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