SpyLoaded Forum









News




Author Topic: Php Email Contact Form  (Read 31311 times)

Offline Mr. Babatunde

  • Administrator
  • Hero Member
  • *****
  • Posts: 7406
  • BBM : C004561AA
Php Email Contact Form
on: July 20, 2025, 02:12:31 PM

To send an email with PHP and the simplest way to do this is to send a text email when a visitor to your website fills out a form.

Create a file name contactform.php
Copy and Paste this in contactform php file

Code: [Select]
<?php
if (isset(
$_POST["submit"])) {
$name $_POST[&#39;name&#39;];
$email $_POST[&#39;email&#39;];
$message $_POST[&#39;message&#39;];
$human intval($_POST[&#39;human&#39;]);
$from $email

// WARNING: Be sure to change this. This is the address that the email will be sent to
$to = &#39;YOUR_SPECIFIED_EMAIL_ADDRESS&#39;; 

$subject "Message from ".$name." ";

$body "From: $name\n E-Mail: $email\n Message:\n $message";
 
// Check if name has been entered
if (!$_POST[&#39;name&#39;]) {
$errName = &#39;Please enter your name&#39;;
}

// Check if email has been entered and is valid
if (!$_POST[&#39;email&#39;] || !filter_var($_POST[&#39;email&#39;], FILTER_VALIDATE_EMAIL)) {
$errEmail = &#39;Please enter a valid email address&#39;;
}

//Check if message has been entered
if (!$_POST[&#39;message&#39;]) {
$errMessage = &#39;Please enter your message&#39;;
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = &#39;Your anti-spam is incorrect&#39;;
}
 
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to$subject$body$from)) {
$result=&#39;<div class="alert alert-success">Thank You! I will be in touch</div>&#39;;
} else {
$result=&#39;<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>&#39;;
}
}
}
?>

Now create a file named index_email.php
Copy and paste this code into the index_email file

Code: [Select]
<?php include("contactform.php"); ?>

<!DOCTYPE html>
<html>
<head>

<title>PHP Email form</title>

<meta name ="viewport" content ="width=device-width, initial-scale=1.0">

<!--Bootstrap CSS CDN-->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!---jQuery CDN-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
       

</head>

<body>

<!-- container for Contact-->
<div class="container-fluid">
<div class ="Contact" id="ContactLink">
<h2 class="text-center large-gray-font">Contact</h2>

<br/>
<br/>

<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<form class="form-horizontal" role="form" method="post" action="index.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST[&#39;name&#39;]); ?>">
<?php echo "<p class=&#39;text-danger&#39;>$errName</p>";?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="[email protected]" value="<?php echo htmlspecialchars($_POST[&#39;email&#39;]); ?>">
<?php echo "<p class=&#39;text-danger&#39;>$errEmail</p>";?>
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST[&#39;message&#39;]);?></textarea>
<?php echo "<p class=&#39;text-danger&#39;>$errMessage</p>";?>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
<?php echo "<p class=&#39;text-danger&#39;>$errHuman</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result?>
</div>
</div>
</form>
</div> <!-- ending div for contact form-->
</div><!-- ending div for container-->
</div><!-- ending div for row -->
</div><!-- ending div for contact -->
</div><!-- ending div for contact container-->



<!--Bootstrap Js CDN-->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>



</body>
</html>

Source: Github.com/ChrishonWyllie










TAG:





 

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Note: this post will not display until it's been approved by a moderator.
Name: Email:
Verification:
"5 eggs" Multiply By "4 eggs" Is what ?:


Close
SimplePortal 2.3.6 © 2008-2014, SimplePortal