What are some common reasons why the PHP mail function may not be working, and how can they be resolved?

Some common reasons why the PHP mail function may not be working include misconfigured mail server settings, incorrect parameters passed to the mail function, or being flagged as spam by the recipient's email server. To resolve these issues, ensure that the mail server settings are correct, double-check the parameters passed to the mail function, and consider adding proper headers to the email to prevent it from being marked as spam.

$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.";
}