What potential security risks are present in the provided PHP code for sending emails?

The provided PHP code is vulnerable to email header injection, which can allow an attacker to manipulate the email headers and potentially send malicious content or spoof the sender's address. To mitigate this risk, it is important to sanitize the input data before using it in the email headers. This can be achieved by validating and sanitizing the input data using PHP functions like filter_var().

// Sanitize the input data for email headers
$to = filter_var($_POST['to'], FILTER_SANITIZE_EMAIL);
$subject = filter_var($_POST['subject'], FILTER_SANITIZE_STRING);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);

// Send the email using sanitized data
mail($to, $subject, $message);