What are the common pitfalls when sending emails using PHP mail function?

One common pitfall when using the PHP mail function is not properly setting the headers, such as the "From" address. This can result in emails being marked as spam or not being delivered at all. To solve this issue, make sure to include headers like "From", "Reply-To", and "MIME-Version" when sending emails.

$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);