What potential security risks should be considered when using the mail() function in PHP?

When using the mail() function in PHP, one potential security risk is the possibility of injection attacks if user input is not properly sanitized. To prevent this, always sanitize user input before using it in the mail() function. Additionally, be cautious of including sensitive information in the email content as it could be intercepted if sent over insecure networks.

// Sanitize user input before using it in the mail() function
$subject = filter_var($_POST['subject'], FILTER_SANITIZE_STRING);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);

// Sending an email using the sanitized input
$to = 'recipient@example.com';
$headers = 'From: sender@example.com';
mail($to, $subject, $message, $headers);