What are the recommended methods for escaping user input and preventing header injection in PHP email scripts?

To escape user input and prevent header injection in PHP email scripts, it is recommended to use the `htmlspecialchars()` function to encode user input before using it in email headers. This function will convert special characters to their HTML entities, preventing any malicious injection attempts.

// Escape user input for email headers
$email = htmlspecialchars($_POST['email']);
$subject = htmlspecialchars($_POST['subject']);
$message = htmlspecialchars($_POST['message']);

// Send email using escaped user input
mail($email, $subject, $message);