Copy link to clipboard
Copied
[Topic edited by moderator.]
Hello,
I have a problem. I put the email form to my website, create the php file and when someone completes the form successfully then clicking the enter button (as submit), the email doesn't show up in my mailbox. Please help me 😄
HTML CODE:
Ask your hosting provider which from-to-email scripts you can use with your hosting plan.
If your server supports PHP and the PHP mail () function, try this contact form. Save as contact.php and upload to your server to test. It goes without saying, you must add your domain and e-mail address to the script where it says //valid address on your web server and //your email address where you wish to receive mail.
<?php
// NOTE: this page must be saved as a .php file.
// And your server
...
Copy link to clipboard
Copied
Ask your hosting provider which from-to-email scripts you can use with your hosting plan.
If your server supports PHP and the PHP mail () function, try this contact form. Save as contact.php and upload to your server to test. It goes without saying, you must add your domain and e-mail address to the script where it says //valid address on your web server and //your email address where you wish to receive mail.
<?php
// NOTE: this page must be saved as a .php file.
// And your server must support PHP 5.3+ and phpMail().
// Define variables and set to empty values
$result = $name = $email = $phone = $human = "";
$errName = $errEmail = $errPhone = $errHuman = "";
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$human = intval($_POST['human']);
//valid address on your web server
$from = 'webmaster@yourdomain.com';
//your email address where you wish to receive mail
$to = 'you@yourdomain.com';
$subject = 'MESSAGE FROM YOUR WEB SITE';
$headers = "From:$from\r\n Reply-to:$email";
$body = "From: $name\n E-Mail: $email\n Phone: $phone";
// Check if name is entered
if (empty($_POST["name"])) {
$errName = "Please enter your name.";
} else {
$name = test_input($_POST["name"]);
}
// Check if email is entered
if (empty($_POST["email"])) {
$errEmail = "Please enter your email address.";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is valid format
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errEmail = "Invalid email format.";
}
}
// Check if phone is entered
if (empty($_POST["phone"])) {
$phone = "";
} else {
$phone = test_input($_POST["phone"]);
}
//Check if simple anti-bot test is entered
if (empty($_POST["human"])) {
$errHuman = "Please enter the sum.";
} else {
//12 = the sum of 6 + 6, if you change the test question, make sure to change this answer.
if ($human !== 12) {
$errHuman = 'Wrong answer. Please try again.';
}
}
// If there are no errors, send form data to email & output results to the form
if (!$errName && !$errEmail && !$errPhone && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success"><h2><span class="glyphicon glyphicon-ok"></span> Message sent!</h2><h3>Thank you for contacting us. Someone will be in touch with you soon.</h3></div>';
} else {
$result='<div class="alert alert-danger"><h2><span class="glyphicon glyphicon-warning-sign"></span> Sorry there was a form processing error.</h2> <h3>Please try again later.</h3></div>';
}
}
}
//for security, sanitize all data inputs
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$data = (filter_var($data, FILTER_SANITIZE_STRING));
return $data;
}
//end form processing script, begin HTML form
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Contact Form, PHP and Bootstrap</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!--Bootstrap CSS-->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<style>
.required {color:red; font-weight:bold}
.center-block {float:none}
.human {margin: 0 0 0 12px}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<div class="col-md-8 center-block">
<h3>Responsive Contact Form</h3>
<p class="required small">* = Required fields</p>
<!--begin HTML Form-->
<form class="form-horizontal" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<!--when submit button is clicked, show results here-->
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result;?>
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-3 control-label"><span class="required">*</span> Name:</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last" value="<?php echo $name; ?>">
<span class="required small"><?php echo $errName; ?></span>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-3 control-label"><span class="required">*</span> Email: </label>
<div class="col-sm-9">
<input type="email" class="form-control" id="email" name="email" placeholder="you@domain.com" value="<?php echo $email; ?>">
<span class="required small"><?php echo $errEmail;?></span>
</div>
</div>
<div class="form-group">
<label for="phone" class="col-sm-3 control-label">Phone: </label>
<div class="col-sm-9">
<input type="tel" class="form-control" id="phone" name="phone" placeholder="(123) 456-7890" value="<?php echo $phone; ?>">
<span class="required small"><?php echo $errPhone;?></span>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-3 control-label"><span class="required">*</span> Human Test:</label>
<div class="col-sm-4">
<h3 class="human">6 + 6 = ?</h3>
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer" value="<?php echo $human; ?>">
<span class="required small"><?php echo $errHuman;?></span>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6 col-sm-offset-3">
<button type="submit" id="submit" name="submit" class="btn-lg btn-primary btn-block">SUBMIT</button>
</div>
</div>
<!--end Form--></form>
<!--end col block--></div>
<!--end col--></div>
<!--end row--></div>
<!--end container--></div>
<!--latest jQuery 3-->
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<!--Latest minified & compiled Bootstrap-->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>
Copy link to clipboard
Copied
I have a question, my server is currently running php 5 point something. Whenever I log in, it warns me that php 5 point something is about to be no longer suported and that I need to switch to php seven point something. I have the option to switch php versions. Will this work with php 7 point something? I did some modification and have created a contact page here: https://www.ironheadcycle.com/pages/contactIHC.php I have not yet made that page public because I'm not sure it will work when the server forces me to switch to php 7.
Copy link to clipboard
Copied
Yes, the php code should still work on a server running php 7.
Copy link to clipboard
Copied
Tank You
Copy link to clipboard
Copied
Thank You very much Nancy, I had the same question and I used your code and it worked!!.. You made my day!!😁👍
regards, Malcolm F
Copy link to clipboard
Copied
Hi @Malcflo,
That tutorial was written quite a while ago. And while it probably still works today for many, it could use updating to the latest Bootstrap, etc...
I must also mention that many hosting plans don't support the PHP mail() function anymore. For this reason, webhosts often recommend using PHPMailer from GitHub.
https://github.com/PHPMailer/PHPMailer