Integrate Mail Function within Form
×
Home Courses Community Support
Integrate Mail Function within Form

As you have learned all about the basics of web form. Now the next step is to integrate mail function in the web form. Below is the code to integrate simple PHP mail function in the code,

<?php
$to      = 'you@yourdomain.com';
$subject = 'Contact Form Enquery';
$message = '<html><body>';
$message .= '<h2>You got a new enquiry. Please login your admin panel to revert the email.</h2>';
$headers = 'From: you@yourdomain.com' . "\r\n" .
    'Reply-To: you@yourdomain.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $message, $headers);
?>


This function will send email to admin that he has received a query. You need to use headers if you want to use HTML template.

Check below code-

<!docty html>
<html>
<head>
	<style>
	.space{
	margin-left:50px;
	margin-bottom:20px;
	}
	.msq{
	margin-left:35px;
	}	
	.btn{
	margin-left:95px;
	margin-top:35px;
	}
	.error{
	color:#FF0000;
	}
	</style>
</head>
<body>

<?php
// define variables and set to empty values
$userNameErr = $userPhoneErr = $userEmailErr = $userMsgErr = "";
$userName = $userPhone = $userEmail = $userMsg = "";
$validate=false;

if ($_SERVER["REQUEST_METHOD"] == "POST") {
	$userName=trim($_POST["userName"]);
	$userPhone=trim($_POST["userPhone"]);
	$userEmail=trim($_POST["userEmail"]);
	$userMsg=trim($_POST["userMsg"]);
	if (empty($userName)) {
		$userNameErr = " Name is required";
	}else if(!preg_match("/^[a-zA-Z ]*$/",$userName)) {	 
		$userNameErr = " Only letters and white space allowed";
	}else if (empty($userPhone)) {
		$userPhoneErr = " Phone number is required";
	}else if(preg_match("/^[a-zA-Z ]*$/",$userPhone)) {
		$userPhoneErr = " Only numeric value is allowed";
	}else if(!filter_var($userEmail, FILTER_VALIDATE_EMAIL)) {
		$userEmailErr = " Enter valid email address";
	}
	else if (empty($userMsg)) {
		$userMsgErr = " Message field can not be blank.";
	}
	else{
		$userName = test_input($userName);
		$userPhone = test_input($userPhone); 		 
		$userEmail = test_input($userEmail); 		 
		$userMsg = test_input($userMsg);
		$validate=true;
	}
}   
function test_input($data) {
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}


?>
<h2>Contact Us</h2>
<form method="post">
<label>Name</label> <input  class="space" name="userName" type="text" ><span class="error"><?php echo $userNameErr;?></span> <br>
<label>Phone</label> <input  class="space" name="userPhone" type="text" ><span class="error"><?php echo $userPhoneErr;?></span> <br>
<label>Email</label> <input  class="space" name="userEmail" type="text" ><span class="error"><?php echo $userEmailErr;?></span> <br>
<label>Message</label> <textarea  class="msq" rows="5" cols="40" name="userMsg"> </textarea><span class="error"><?php echo $userMsgErr;?></span> <br>
<input class="btn" type="submit" name="submit">
</form>

<?php if($validate){?>

<?php
$to      = 'you@yourdomain.com';
$subject = 'Contact Form Enquery';
$message = '<html><body>';
$message .= '<h2>You got a new enquiry. Please login your admin panel to revert the email.</h2>';
$headers = 'From: you@yourdomain.com' . "\r\n" .
    'Reply-To: you@yourdomain.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $message, $headers);
?>

<?php }?>
</body>
</html>

 

In the same way you can send complete data on the email.

 

 

← Preview

Validating Form

Next →

Creating a Database

NOTES

Our mission is to provide highly effective and quality education via innovative solutions. Geeksdemy look forward to bridge the gap between in-demand technology and academics in order to deliver innovative, easy, interesting and affordable learning across the Globe.

Courses

  • Python
  • Game Development with Unity 3D
  • Arduino
  • PCB & Circuit Designing
  • Robotics and Embedded C
  • 8051/8052 Embedded Systems
  • QuadCopter & UAV
  • MATLAB with Robotics
  • Raspberry Pi
Learn On the Go!
Get the Android App
Get the iOS App