How to Send Email With PHP on Button Click?

php mail on button click

To prevent duplicate data being send on page load or on going back to previous or next pages we have to set condtion in php mail that unless a submit button is clicked no data is being passed. To achieve this we will use isset().

It is an inbuilt function for checking if a variable is set or not.To send email we can either use php mail function or phpmailer. In this blog we will learn to do it through PHP mail()

For checking whether the submit button is clicked or not we will use the following condition using the isset function.Below is the example code for using isset function in PHP.

if(isset($_POST['submit'])){
//statement;
}

To see how we can practically use the above condition we will create a registration form using HTML first to get the values from users. We will name this file as form.php

Source code for simple PHP and HTML form

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Form to Send PHP Mail</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<style>
.frm{
border:1px solid grey;
padding:15px;
}
</style>
</head>
<body>

<div class="container">
	<div class="row">
    <div class="col-md-4 frm">
  <h2>Form to Send PHP Mail</h2>
  <form action="mail.php" method="post" >
    <div class="form-group">
      <label for="name">Name:</label>
      <input type="text" class="form-control" id="name" placeholder="Enter name" name="name">
    </div>
    <div class="form-group">
      <label for="mail">Email:</label>
      <input type="email" class="form-control" id="mail" placeholder="Enter Email" name="email">
    </div>
    <button type="submit" name ="submit" class="btn btn-default">Submit</button>
  </form>
</div></div></div>

</body>
</html>
<?php
//condition to check whether submit button is clicked or not
if(isset($_POST['submit'])){
	
$name = $_POST['name'];
$email =$_POST['email'];

$to = "abc@mail.com"; //receivers email address
$subject ="my subject"; //subject of the mail
$message ="php test mail";//mail content or message body

mail($to, $subject, $message) or die("Error!");
echo "<script>alert('Thank You! Request Submited  Successfully.');</script>";
}
else{
	echo"error";
}
?>

now we will create another file as email.php and get the input values entered by the user while filling the registration form which are passed from form.php on submit of button

Source code for getting form data on button click

Note: only input fields with name attribute will have their values passed on click of submit button

source code to send mail with extra headers on button click

<?php
//condition to check whether submit button is clicked or not
if(isset($_POST['submit'])){
	
$name = $_POST['name'];
$email =$_POST['email'];

$to = "test@mail.com"; //receivers email address
$subject ="my subject"; //subject of the mail
$message ="php test mail";//mail content or message body
$headers = "From:$email" . "\r\n".
	   "CC:abc@mail.com" ."\r\n".
	   "BCC:abcd@mail.com";

mail($to, $subject, $message, $headers) or die("Error!");
echo "<script>alert('Thank You! Request Submited  Successfully.');</script>";
}
else{
	echo"error";
}
?>

or you can use extra parameters as

<?php
//condition to check whether submit button is clicked or not
if(isset($_POST['submit'])){
	
$name = $_POST['name'];
$email =$_POST['email'];

$to = "test@mail.com"; //receivers email address
$subject ="my subject"; //subject of the mail
$message ="php test mail";//mail content or message body
$headers = "From:$email" . "\r\n";
$headers =	"CC:abc@mail.com" ."\r\n";
$headers =	"BCC:abcd@mail.com";

mail($to, $subject, $message, $headers) or die("Error!");
echo "<script>alert('Thank You! Request Submited  Successfully.');</script>";
}
else{
	echo"error";
}
?>

17 thoughts on “How to Send Email With PHP on Button Click?

Leave a Reply

Your email address will not be published. Required fields are marked *