How can a PHP developer ensure that email notifications are properly sent from a contact form with CAPTCHA validation?

To ensure that email notifications are properly sent from a contact form with CAPTCHA validation, the PHP developer can implement server-side validation to check if the CAPTCHA input matches the expected value before sending the email. This helps prevent spam submissions and ensures that only legitimate users can submit the form.

<?php

// Validate CAPTCHA input
$expectedCaptcha = "12345"; // Expected CAPTCHA value
$captchaInput = $_POST['captcha']; // CAPTCHA input from the form

if ($captchaInput != $expectedCaptcha) {
    // CAPTCHA validation failed
    echo "CAPTCHA validation failed. Please try again.";
} else {
    // CAPTCHA validation passed, proceed with sending email
    $to = "recipient@example.com";
    $subject = "Contact Form Submission";
    $message = "Name: " . $_POST['name'] . "\n";
    $message .= "Email: " . $_POST['email'] . "\n";
    $message .= "Message: " . $_POST['message'];

    // Send email
    mail($to, $subject, $message);

    echo "Email sent successfully!";
}

?>