What best practices should be followed when handling form submissions in PHP to prevent issues with email validation and header definitions?

When handling form submissions in PHP, it is important to properly validate email addresses and prevent header injections to avoid security vulnerabilities. To ensure email validation, you can use PHP's built-in filter_var function with the FILTER_VALIDATE_EMAIL filter. To prevent header injections, sanitize user input using functions like htmlspecialchars or htmlentities to encode special characters.

// Validate email address
$email = $_POST['email'];
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // Handle invalid email address
}

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

// Process form submission
// Send email or save to database