How can PHP be used to trigger an email only when a specific action is taken on a website, such as submitting a form?

To trigger an email only when a specific action is taken on a website, such as submitting a form, you can use PHP to check for the action and then send the email accordingly. One way to achieve this is by adding a condition in the form submission handler that checks if the specific action has been taken before sending the email.

if(isset($_POST['submit'])) {
    // Process form data
    
    // Check if specific action is taken
    if($_POST['specific_action'] == 'trigger_email') {
        // Send email
        $to = 'recipient@example.com';
        $subject = 'Specific action triggered';
        $message = 'The specific action has been taken on the website.';
        $headers = 'From: sender@example.com';
        
        mail($to, $subject, $message, $headers);
    }
}