What are common pitfalls or errors that may occur when using the mail() function in PHP?

One common pitfall when using the mail() function in PHP is that emails may be marked as spam by the recipient's email provider. To avoid this, make sure to set proper headers, such as the "From" header, and include a valid sender email address. Additionally, ensure that the content of the email is not flagged as spammy by avoiding excessive use of capital letters or special characters.

$to = "recipient@example.com";
$subject = "Subject of the email";
$message = "This is the content of the email";
$headers = "From: sender@example.com\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";

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