0

Warning I'm pretty new to php

So I made a basic contact form. It appears to be working but it's not sending any mail to my email address. I've made simple contact forms based on the model here and they work. The form is not throwing any errors and I added a redirect script if it post successfully. After submission it redirects as intended. What am I missing? Any help would be greatly appreciated!

$error = "";

if ($_POST) {

    // Validation       

  if (!$_POST["name"]) {

      $error .= "The name field is required.<br>";

  }

    if (!$_POST["email"]) {

        $error .= "An email address is required.<br>";

    }

    if (!$_POST["content"]) {

        $error .= "The content field is required.<br>";

    }


    if ($_POST['email'] && filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) {

        $error .= "The email address is invalid.<br>";

    }

    if ($error != "") {

        $error = '<div class="alert" role="alert"><p>There were error(s) in your form:</p>' . $error . '</div>';

    } else {

         // Changed $emailTo for the sake of posting publicly. On my site it has my actual email

        $emailTo = "email@email.com";

        $subject = "Contact Inquiry";

        $userName = $_POST['name'];

        $userPhone = $_POST['phone'];

        $userWebsite = $_POST['website'];

        $userEmail = $_POST['email'];

        $userContent = $_POST['content'];

        $headers = "From: ".$_POST['email'];

        $email_message = "Form details below.\n\n";

        $email_message .= "Name: ". $userName ."\n";
        $email_message .= "Email: ". $userEmail ."\n";
        $email_message .= "Phone: ". $userPhone ."\n";
        $email_message .= "Website: ". $userWebsite ."\n";
        $email_message .= "Message: ". $userContent ."\n";

        if (mail($emailTo, $subject, $email_message, $headers)) {

          ?>

            // Again changed for the sake of posting publicly

            <script> window.location = 'example.com' </script>

          <?php


        } else {

            $error = '<div class="alert" role="alert"><p><strong>Your message couldn\'t be sent - please try again later</div>';


        }

    }



}


<form method="post">
    <div class="fields-container clear-fix">
      <div class="left">
        <input type="text" class="form-control" id="name" name="name" placeholder="Name">
        <input type="text" class="form-control" id="phone" name="phone" placeholder="Phone">
      </div>
      <div class="right">
        <input type="email" class="form-control" id="email" name="email" placeholder="Email">
        <input type="text" class="form-control" id="website" name="website" placeholder="Website">
      </div>
    </div>
    <textarea id="form-content" name="content" rows="3" placeholder="Tell me about your project. What is it? What are you hopint to accomplish? How can I help? Budget details are always appreciated"></textarea>
    <button type="submit" id="submit" class="">Submit</button>
  </form>
JMDev
  • 1
  • 1
  • you're on localhost? Got a mail-server set up? – Jeff Feb 27 '18 at 22:28
  • Make sure the server has mail function available. One hosting provider disabled it for me once due to SPAM accusations (made by wordpress in my case). – Michał Skrzypek Feb 27 '18 at 22:28
  • It's not on localhost. It's on the actual server. The server does have the mail function available. When I copy and paste another form I've made using the same structure, the form works and sends the mail. – JMDev Feb 27 '18 at 22:41

0 Answers0