What are the potential pitfalls of specifying a sender in the mail() function in PHP?

Specifying a sender in the mail() function in PHP can lead to emails being marked as spam or rejected by the recipient's email server if the sender domain does not match the actual sending server. To avoid this issue, it is recommended to set the "From" header in the email headers instead of using the fifth parameter in the mail() function.

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

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