When using the mail() function in PHP, what are best practices for formatting the "From" header parameter to avoid errors?

When using the mail() function in PHP, it's important to properly format the "From" header parameter to avoid errors. The "From" header should include both the email address and the sender's name in the format: "Sender Name <sender@example.com>". This helps prevent emails from being marked as spam or rejected by the recipient's mail server.

$to = &quot;recipient@example.com&quot;;
$subject = &quot;Test Email&quot;;
$message = &quot;This is a test email.&quot;;
$headers = &quot;From: Sender Name &lt;sender@example.com&gt;&quot;;

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