How can the mailto window be closed after using header() in PHP?

After using the header() function in PHP to redirect to a mailto link, the mailto window may remain open after the email is sent. To close the mailto window automatically after sending the email, you can use JavaScript in conjunction with the header() function. By adding a small script that closes the window after a short delay, you can ensure that the mailto window closes without requiring any user interaction.

<?php
// Redirect to mailto link
header("Location: mailto:example@example.com");

// Close the mailto window after a short delay using JavaScript
echo '<script type="text/javascript">
        setTimeout(function() {
            window.close();
        }, 1000); // Close after 1 second
      </script>';
?>