What potential issues can arise when trying to send emails using the mail() function in PHP?

One potential issue when using the mail() function in PHP is that emails may be marked as spam by recipients' email servers due to missing headers or improper formatting. To prevent this, you should include necessary headers like From, Reply-To, and Content-Type in the email headers. Additionally, make sure the email content is properly formatted and includes a valid subject line.

$to = "recipient@example.com";
$subject = "Subject of the email";
$message = "This is the body of the 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);