Skip to main content
kacpero12956336
Participant
October 27, 2019
Answered

sending to e-mail from a contact form

  • October 27, 2019
  • 1 reply
  • 4226 views

[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:

 

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>BORING TEE</title>
<link href="style.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://use.typekit.net/gcc6ije.css">
 
<!-- Global site tag (gtag.js) - Google Analytics -->
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
 
  gtag('config', 'UA-109231661-2');
</script>
 
</head>
 
<body style="background-color:#000000;">
<div class="row2">
  <div class="column2">
    <img src="boringtee1.png" alt="Shop" class="smileytee">
  </div>
  <div class="column2">
    <img src="boringtee2.png" alt="Shop" class="smileytee1">
 </div>
</div>
<h5>pre order available soon</h5>
<h6>Limited to 100 pieces</h6>
<h6>shipping worldwide</h6>
<form class="email" action="email.php" method="post">
<input type="text" class="enter" style="background-color: #000000" name="email" placeholder="ENTER EMAIL FOR UPDATES">
</form>
<a href="shop.html">
<img src="back_button.png" class="back1" alt="backbutton"></a>
 
<script type="text/javascript">
    $(document).ready(function () {
 
        var makeAllFormSubmitOnEnter = function () {
            $('form input, form select').live('keypress', function (e) {
                if (e.which && e.which == 13) {
                    $(this).parents('form').submit();
                    return false;
                } else {
                    return true;
                }
            });
        };
 
        makeAllFormSubmitOnEnter();
    });
</script>
</body>
</html>
 
PHP CODE
 
<?php
 
if (isset($_POST['submit'])) {
$mailFrom = $_POST['email'];
 
$mailTo = "contact@22c11.club";
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from customer";
 
mail($mailTo, $txt, $headers);
}
 
$conn = new mysqli ($mailTo, $headers, $txt);
 
 
 
?>
This topic has been closed for replies.
Correct answer Nancy OShea

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>

 

 

 

1 reply

Nancy OShea
Community Expert
Nancy OSheaCommunity ExpertCorrect answer
Community Expert
October 27, 2019

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>

 

 

 

Nancy O'Shea— Product User & Community Expert
Participant
May 14, 2021

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.

Legend
May 14, 2021

Yes, the php code should still work on a server running php 7.