When using the mail() function in PHP, what specific requirements does the server need to meet for successful email delivery?

When using the mail() function in PHP for sending emails, the server needs to be properly configured to handle outgoing emails. This includes having a working mail server such as Sendmail or Postfix installed and configured, as well as ensuring that the server's firewall settings allow outgoing SMTP connections. Additionally, the server's DNS settings should have proper MX records set up for the domain from which the emails are being sent.

// Example PHP code snippet for sending an email using the mail() function
$to = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email.";
$headers = "From: sender@example.com";

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