Are there any best practices for handling email headers, such as the "From" field, in PHP scripts?

When handling email headers in PHP scripts, it is important to properly sanitize and validate the "From" field to prevent email header injection attacks. One best practice is to use the built-in PHP function filter_var() with the FILTER_VALIDATE_EMAIL filter to validate the email address before using it in the email headers.

$from_email = filter_var($_POST['from_email'], FILTER_VALIDATE_EMAIL);
$from_name = $_POST['from_name'];

$headers = "From: $from_name <$from_email>\r\n";

// Additional headers and email sending code