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

One common issue when using the mail() function in PHP for sending emails is that emails may end up in the recipient's spam folder. This can be caused by not setting proper headers or sending emails from a server with a poor reputation. To solve this, make sure to set the "From" header correctly and consider using a reputable email service provider for sending emails.

$to = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email.";

$headers = "From: yourname@example.com\r\n";
$headers .= "Reply-To: yourname@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);