What security measures should be implemented in a PHP contact form to prevent email injection?

Email injection can occur when malicious users input special characters into the contact form fields, allowing them to inject additional headers into the email being sent. To prevent this, it is important to sanitize user input before using it to construct the email message. This can be done by using PHP's `filter_var` function with the `FILTER_SANITIZE_EMAIL` flag to validate and sanitize the email address input.

// Sanitize email input to prevent email injection
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);

// Additional validation if needed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // Handle invalid email address
}

// Use the sanitized email address in the email message