Is there a recommended approach or method for handling conditional email sending based on the validation status of user input in PHP?

To handle conditional email sending based on the validation status of user input in PHP, you can use an if statement to check the validation status before sending the email. If the user input passes validation, the email will be sent; otherwise, an error message can be displayed to the user.

<?php
// Validate user input
if ($input_validated) {
    // Send email
    $to = "recipient@example.com";
    $subject = "Subject";
    $message = "Email message";
    $headers = "From: sender@example.com";
    
    if (mail($to, $subject, $message, $headers)) {
        echo "Email sent successfully";
    } else {
        echo "Email sending failed";
    }
} else {
    echo "Invalid input, email not sent";
}
?>