How can developers prevent email header injections and other vulnerabilities when processing form data in PHP?

Email header injections occur when user input is not properly sanitized before being used in email headers, allowing malicious users to inject additional headers or modify existing ones. To prevent this vulnerability, developers should always sanitize user input using functions like `filter_var()` or `htmlspecialchars()` to ensure that no malicious code is included in the email headers.

// Sanitize user input to prevent email header injections
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$name = htmlspecialchars($_POST['name']);
$message = htmlspecialchars($_POST['message']);

// Send email using sanitized input
$to = 'recipient@example.com';
$subject = 'Contact Form Submission';
$headers = 'From: ' . $name . ' <' . $email . '>';
$body = $message;

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