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

One common issue when using the mail() function in PHP to send emails is that emails may end up in the recipient's spam folder. To solve this issue, you can set additional headers such as "From" and "Reply-To" headers to ensure that the email appears legitimate to email servers.

$to = 'recipient@example.com';
$subject = 'Subject of the email';
$message = 'This is the body of the email';

$headers = 'From: yourname@example.com' . "\r\n" .
    'Reply-To: yourname@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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