0

I have made my website live and decided to test out my contact form, which is a simple php script I found online. It seems simple enough, but I can't figure out why I am getting the die "Error!" when I fill in all the fields and click submit on my live website. I don't have any experience working with php, so any help here would be very appreciated! PHP:

<?php
  $name = $_POST['name'];
  $email = $_POST['email'];
  $subject = $_POST['subject'];
  $message = $_POST['message'];
  $formcontent="From: $name \n Email: $email \n Subject: $subject \n Message: $message";
  $recipient = "email@gmail.com";
  $subject = "Customer Contact Message";
  $mailheader = "From: $email \r\n";
  mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
  echo "Thank you for contacting us.";
?>

HTML:

<form class="contact-form" id="contact-form" action="contactform.php" method="post">
  <div class="col-md-6">
    <input placeholder="Name" name="name" type="text">
  </div>

  <div class="col-md-6">
    <input placeholder="Email" name="email" type="text">
  </div>

  <div class="col-md-12">
    <input placeholder="Subject" name="subject" type="text">
  </div>
  <div class="col-md-12">
    <textarea placeholder="Message" name="message"></textarea>
  </div>
  <div class="col-md-12">
    <button class="btn submit-btn btn-primary" type="submit">Send Message</button>
  </div>
</form>
Ivan
  • 34,531
  • 8
  • 55
  • 100
ellaaur
  • 9
  • 3
  • 1
    it just means that the `mail()` function returned false, so there's something wrong in your mail settings. – Florian Humblot Aug 29 '17 at 13:24
  • 2
    Never trust the user input, validate/sanirize all the `$_POST` values. – Script47 Aug 29 '17 at 13:25
  • It's ok to do this as a fun little experiment, but you should never deploy this code. It can easily be abused by bad guys to send spam. – Scott C Wilson Aug 29 '17 at 13:25
  • Output your errors on the screen to give you better error reporting (see: https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) or better yet use a debugger like xdebug. – Robert Wade Aug 29 '17 at 13:25
  • 1
    Possible duplicate of [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – aynber Aug 29 '17 at 13:25
  • Trying using phpMailer https://github.com/PHPMailer/PHPMailer – nkengbeza Aug 29 '17 at 13:27

2 Answers2

0
<button class="btn submit-btn btn-primary" type="submit">Send 
Message</button>
instead use this:

<input type="submit" class="btn submit-btn btn-primary" value="Send Message" 
name="submit">

And your php script will be like this:
<?php
    if(isset($_POST['submit'])){
       $name = $_POST['name'];
       $email = $_POST['email'];
       $subject = $_POST['subject'];
       $message = $_POST['message'];
       $formcontent="From: $name \n Email: $email \n Subject: $subject \n 
       Message: $message";
       $recipient = "email@gmail.com";
       $subject = "Customer Contact Message";
       $mailheader = "From: $email \r\n";
       mail($recipient, $subject, $formcontent, $mailheader) or 
       die("Error!");
       echo "Thank you for contacting us.";
   }    
?>
AruniAB
  • 1
  • 2
0

When I first started to learn PHP I found setting up an email form troublesome, I would recommend trying out the following service: https://formspree.io/ it simplifies the whole process for you for free.