What is the correct way to trigger an email to be sent when a button is clicked in PHP?
To trigger an email to be sent when a button is clicked in PHP, you can use a combination of HTML and PHP code. You can create a form with a submit button that, when clicked, will call a PHP script to send the email using the `mail()` function. Make sure to properly set the `to`, `subject`, `message`, and `headers` parameters in the `mail()` function to ensure the email is sent correctly.
<?php
if(isset($_POST['submit'])) {
$to = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email.";
$headers = "From: sender@example.com";
mail($to, $subject, $message, $headers);
echo "Email sent successfully.";
}
?>
<!DOCTYPE html>
<html>
<body>
<form method="post">
<input type="submit" name="submit" value="Send Email">
</form>
</body>
</html>