Button click send mail in php
Here is an example of how you can send an email using PHP when a button is clicked:
<?php
// Configuration
$to = '[email protected]';
$subject = 'Test Email';
$message = 'This is a test email sent from a button click';
// Check if the form has been submitted
if (isset($_POST['send_email'])) {
// Send the email
$headers = 'From: [email protected]'. "\r\n".
'Reply-To: [email protected]'. "\r\n".
'MIME-Version: 1.0'. "\r\n".
'Content-Type: text/plain; charset=UTF-8';
mail($to, $subject, $message, $headers);
echo 'Email sent successfully!';
} else {
// Display the form
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<input type="submit" name="send_email" value="Send Email">
</form>
<?php
}
?>
This code checks if the send_email
form has been submitted by checking if the $_POST
superglobal array contains a value for the send_email
key. If it does, it sends the email using the mail()
function.
The mail()
function takes four arguments:
$to
: the recipient's email address$subject
: the subject of the email$message
: the body of the email$headers
: the headers of the email
In this example, the headers are set to include the sender's email address, reply-to email address, MIME version, and content type.
When the form is submitted, the email is sent and a success message is displayed. If the form is not submitted, the form is displayed and the user can click the "Send Email" button to send the email.
Note that this is a very basic example and you may want to add more functionality to your email sending script, such as error handling and validation.