What are common issues with using the mail() function in PHP?

Common issues with using the mail() function in PHP include emails being marked as spam, emails not being delivered, and lack of error handling. To solve these issues, you can set proper headers, use a reliable mail server, and implement error handling to track any issues with 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 .= "X-Mailer: PHP/" . phpversion();

if (mail($to, $subject, $message, $headers)) {
    echo "Email sent successfully.";
} else {
    echo "Email sending failed.";
}