What best practices should be followed when validating form input in PHP to prevent issues like emails not being sent?

When validating form input in PHP to prevent issues like emails not being sent, it is important to ensure that the email address provided is in a valid format. This can be done by using PHP's built-in filter_var function with the FILTER_VALIDATE_EMAIL flag. Additionally, it is recommended to sanitize the input to prevent any malicious code from being executed.

// Validate and sanitize email input
$email = $_POST['email'];

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $sanitized_email = filter_var($email, FILTER_SANITIZE_EMAIL);
    // Proceed with sending the email
} else {
    // Handle invalid email input
}