What are the potential reasons for PHP emails failing to deliver due to From and Return-Path mismatch?

When PHP emails fail to deliver due to From and Return-Path mismatch, it is typically because the Return-Path header does not match the domain of the email address in the From header. To solve this issue, ensure that the Return-Path header is set to a valid email address within the same domain as the From address.

$from = "sender@example.com";
$to = "recipient@example.com";
$subject = "Subject";
$message = "Message";

$headers = "From: $from\r\n";
$headers .= "Return-Path: $from\r\n";

mail($to, $subject, $message, $headers);