Php表单通过电子邮件发送给我,但不发送表单内容

时间:2016-07-26 20:35:33

标签: php html forms

我无法让表单向我发送用户填写的表单数据。下面是表单和PHP的HTML。表单正在发送给我,但我不确定我是否正确设置了HTML和PHP。我已经尝试过挖掘答案,但我在PHP中没有很多知识,而且大多数其他问题都有更复杂的形式。试着从我寻找答案中学到的东西,我想我已经把一切都弄清楚了。请帮忙。

    <form  method="post" action="contact2.php" enctype="text/plain">
      <label for="name">Name</label>
      <input name="name" type="text" id="name">
      <label for="email">Email Address</label>
      <input name="email" type="text" id="email">
      <label for="phone">Phone Number</label>
      <input name="phone" type="text" id="phone">
      <label for="comments">Comments</label>
      <textarea name="comments" id="comments" rows="5" cols="38"><?php echo $comment;?></textarea>
      <input type="submit" value="Submit">
    </form>


<?php
$to      = 'myemail@myemail.com';
$subject = "Message from my internet form";
$message .= $_POST['name'];
$message .= $_POST['email'];
$message .= $_POST['phone'];
$message .= $_POST['comments'];
$headers = 'From: ' . $_POST['name'] . $_POST['email']  . "\r\n" .
    'Reply-To: notsure@whatgoeshere.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
  header('Location: http://mypage.com/thankyou.html');
exit();
?>

编辑:更新了上面的代码以反映我的更改。我仍然没有收到表单信息到我的电子邮件,我收到的电子邮件看起来很有趣,“www@myemailprovider.com”它以www开头,看起来像是来自我,而不是用户填写表格。

edit2:现在可以使用,所有代码都会更新。对于好奇,我能够通过在PHP表单中放置一个表来格式化传入的消息。它看起来像这样:

<?php
$to      = 'you@youremail.com';
$subject = "This is the subject line for the email you receive";
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$_POST['Name']." <".$_POST['email'].">\r\n";
$headers .= "Reply-To: ".$_POST["email"]."\r\n";

    'X-Mailer: PHP/' . phpversion();
     $message = '
  <table>
     <tr><td>Name:</td><td>'.$name.'</td></tr>
     <tr><td>Email:</td><td>'.$email.'</td></tr>
     <tr><td>Phone:</td><td>'.$phone.'</td></tr>
     <tr><td>Comments:</td><td>'.$comments.'</td></tr>
  </table>';

mail($to, $subject, $message, $headers);
  header('Location: http://yourpage.com/thankyou.html');
exit();
?>

1 个答案:

答案 0 :(得分:1)

POST个变量要求您的输入中包含name属性。

试试这个:

<form  method="post" action="contact2.php" enctype="text/plain">
      <label for="name">Name</label>
      <input name="name" type="text" id="name">
      <label for="email">Email Address</label>
      <input name="email" type="text" id="email">
      <label for="phone">Phone Number</label>
      <input name="phone" type="text" id="phone">
      <label for="comments">Comments</label>
      <textarea name="comments" id="comments" rows="5" cols="38"><?php echo $comment;?></textarea>
<input type="submit" value="Submit">
</form>