Are there any security considerations to keep in mind when using the mail() function in PHP?

When using the mail() function in PHP, it is important to sanitize user input to prevent injection attacks. Additionally, it is recommended to set proper headers to prevent email spoofing and to avoid using sensitive information in the email body.

$to = 'recipient@example.com';
$subject = 'Test Email';
$message = 'This is a test email';
$headers = 'From: sender@example.com' . "\r\n" .
    'Reply-To: sender@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

$cleaned_message = filter_var($message, FILTER_SANITIZE_STRING);

mail($to, $subject, $cleaned_message, $headers);