How can different server configurations affect the successful sending of emails using PHP's "mail()" function?

Different server configurations can affect the successful sending of emails using PHP's "mail()" function due to restrictions or settings imposed by the server. To ensure successful email delivery, you can specify additional headers in the mail function, such as the "From" address, and ensure that the server's settings allow for outgoing emails.

$to = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email.";
$headers = "From: sender@example.com\r\n";

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