What are common pitfalls when using the PHP mail() function to send emails?

Common pitfalls when using the PHP mail() function include emails being marked as spam due to improper headers, emails not being delivered due to server configuration issues, and potential security vulnerabilities if user input is not properly sanitized. To ensure emails are not marked as spam, make sure to include proper headers such as From, Reply-To, and MIME type. Additionally, consider using a library like PHPMailer for more advanced email functionality and better deliverability.

$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 .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

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