联系表格不发送电子邮件php

时间:2018-02-27 22:26:04

标签: php contact-form

警告我是php的新手

所以我做了一个基本的联系表格。它似乎正在工作,但它没有发送任何邮件到我的电子邮件地址。我在这里根据模型制作了简单的联系表格,并且它们有效。表单没有丢失任何错误,如果成功发布,我添加了重定向脚本。提交后,它会按预期重定向。我错过了什么?任何帮助将不胜感激!

$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>

0 个答案:

没有答案