What are common reasons for PHP mail functions to run without errors but emails not being delivered?

One common reason for PHP mail functions to run without errors but emails not being delivered is due to misconfigured mail server settings. Ensure that the SMTP server, port, username, and password are correctly set in the PHP mail function. Additionally, check for any spam filters or firewall settings that may be blocking the emails from being delivered.

<?php
$to = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email.";
$headers = "From: sender@example.com";

ini_set("SMTP", "mail.example.com");
ini_set("smtp_port", "25");

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