How can improper use of control characters in message headers affect PHP functions like mail()?

Improper use of control characters in message headers can lead to security vulnerabilities such as header injection attacks. To prevent this, it is important to properly sanitize and validate user input before using it in mail headers. One way to do this is by using the `filter_var()` function with the `FILTER_SANITIZE_STRING` filter to clean the input.

$to = 'recipient@example.com';
$subject = 'Test Subject';
$message = 'This is a test message';
$headers = 'From: sender@example.com' . "\r\n";
$headers .= 'Reply-To: sender@example.com' . "\r\n";

// Sanitize and validate headers
$clean_headers = filter_var($headers, FILTER_SANITIZE_STRING);

// Send the email
mail($to, $subject, $message, $clean_headers);