将表单信息发送到电子邮件

时间:2015-03-15 13:58:09

标签: php forms email submit

我有一个表单,可以将信息发送到php doc。测试时,我提交并显示数组,但不会转到电子邮件地址。

<?php require_once("/includes/fbcheader.php"); 
?>
<div class="comments">
     <h3> We Welcome All Comments </h3>
   <form action="/php/contact-send.php" method="post" 
                                            id="contact-form" class="form">
     <p class="input-block"> 
          <label for="form-name" >Name</label>
          <input type="text" value name="name" id="form-name" 
               autofocus placeholder="Please enter name" required>
          </p>
        <p class="input-block">
            <label for="form-email" >Email</label>
            <input type="email" value name="email" id="form-email" 
                 required placeholder="Please enter E-Address">
            <input type="hidden" value name="age" id="age">
        </p>
        <p class="input-block">
            <label for="form-subject" >Subject</label>
            <input type="text" value name="subject" id="form-subject"
                 required placeholder="Please enter subject">
        </p>
        <p class="textarea-block">
            <label for="form-message">Comment</label>
            <textarea name="message" id="form-message" 
                 cols="70" rows="10"></textarea>
        </p>
   <div class="clear"></div>
                <input type="hidden" name="firstname" id="firstname">
                <input type="submit" value="Send" id="form-submit">
                <p class="hide" id="response"></p>
            <div class="hide">
                <label for="spam-check">Do not fill out this field</label>
                <input name="spam-check" type="text" value id="spam-check">
            </div>
    </form>
</div>
</body>
</html>

这是发送php执行代码

<html>
<head>
 <title>Contact-Send</title>
</head>
 <body>
   <?php
      if(null!==($_POST["name"])) {
          $name = $_POST["name"];
         /* echo "Please enter 'Name'<br />"; */
      } else {
          $name = "";
      }
      if(null!==($email = $_POST["email"])) {
          $email = $_POST["email"];
          /* echo "Please enter 'E_Mail'<br />"; */
      } else {
          $email = "";
      }
      if(null!==($email = $_POST["subject"])) {
          $form_email = $_POST["subject"];
          /* echo "Please enter 'Subject'<br />"; */
      } else {
          $subject = "";
      }
    if(null!==($message = $_POST["message"])) {
        $message = $_POST["message"];
        /* echo "Please enter your message<br />"; */
    }   else {
        $message = "";
    }
    /*echo "{$name}, {$email}, {$message}";*/

  $email_from = '$Email';

  $email_subject = "New Form submission";

  $email_body = "You have received a new message from the user $name.\n".
                        "Here is the message:\n $message".

  $to = "me@myaddress.com";
   ?>
   <br>
     <?php
      $headers = "From: $email_from \r\n";

      $headers .= "Reply-To: $form_email \r\n";

    mail($to,$email_subject,$email_body,$headers);

    header("thank-you.html");
   ?>

   </body>
  </html>

在提交时,我只是让数组显示输入字段的内容

1 个答案:

答案 0 :(得分:0)

快速浏览一下您的PHP代码我看到以下内容:

这样:if(null!==($email = $_POST["email"]))将失败,因为您正在对任何内容进行检查。它应该是:if(null!==($_POST["email"]))

这里有一个未定义的变量:$email_from = '$Email';这只会输出“$ Email”。它应该是$email_from = $email;(根据您的代码)。但是你只是在这里重新分配你根本不需要做的变量。

您的concatenating $email_body$to地址。

header("thank-you.html");将失败,因为根据您的代码,输出已在此处<?php(第6行)开始。

你应该针对 contact-send.php

更接近这一点。
<?php
      if(null!==($_POST["name"])) {
          $name = $_POST["name"];
         /* echo "Please enter 'Name'<br />"; */
      } else {
          $name = "";
      }
      if(null!==($_POST["email"])) {
          $email = $_POST["email"];
          /* echo "Please enter 'E_Mail'<br />"; */
      } else {
          $email = "";
      }
      if(null!==($_POST["subject"])) {
          $form_email = $_POST["subject"];
          /* echo "Please enter 'Subject'<br />"; */
      } else {
          $subject = "";
      }
    if(null!==($_POST["message"])) {
        $message = $_POST["message"];
        /* echo "Please enter your message<br />"; */
    }   else {
        $message = "";
    }
    /*echo "{$name}, {$email}, {$message}";*/

  $email_subject = "New Form submission";

  $email_body = "You have received a new message from the user $name.\n".
                "Here is the message:\n $message";

  $to = "me@myaddress.com";
    $headers = "From: $email \r\n";
    $headers .= "Reply-To: $email \r\n";
    mail($to,$email_subject,$email_body,$headers);
    header("Location: thank-you.html");
   ?>

请务必将$to设置为您的电子邮件地址!