How effective is the code construct in preventing mail injections?
Mail injections can occur when user input is not properly sanitized before being used in email headers. To prevent mail injections, it is important to validate and sanitize user input before including it in email headers. This can be done by using PHP's `filter_var()` function with the `FILTER_SANITIZE_EMAIL` filter to clean the email address input.
// Sanitize email input to prevent mail injections
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
// Send email using sanitized email address
$to = $email;
$subject = 'Subject';
$message = 'Message';
$headers = 'From: your_email@example.com' . "\r\n" .
'Reply-To: your_email@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);