What are the risks associated with E-Mail-Injection in PHP scripts and how can they be prevented?

E-Mail-Injection in PHP scripts can allow attackers to inject malicious code into email headers, potentially leading to spamming, phishing, or unauthorized access to sensitive information. To prevent E-Mail-Injection, it is crucial to sanitize user input and validate email addresses before using them in email headers.

// Sanitize and validate email address before using it in email headers
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // Proceed with sending the email
} else {
    // Handle invalid email address
}