HTML 5表单到PHP邮件脚本发送带有未定义变量错误的空白邮件


HTML 5 form to PHP email script sends blank email with undefined variable error

我是HTML 5和PHP的新手,我一直在尝试获得一个测试电子邮件发送给我,其中包含表单中的信息。我得到一个空白的电子邮件,当检查Apache日志它显示未定义的变量从PHP表单处理程序。

HTML表单:

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" type="text/css" href="formstyleb.css">
    <link rel="stylesheet" href="css/validationEngine.jquery.css" type="text/css"/>
    <script type="text/javascript" src="jquery-1.9.1.min.js"></script>
    <script src="jquery.validate.min.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<form action="formemail.php" method="post" enctype="text/plain" id="contact">
   <fieldset>
     <legend>Your Contact Details:</legend>
<div>
<label for="author">Name:(Required)</label>
<input name="author" class="required name" id="author" type="text"
title="Please Enter Your Name!">
</div>
   <div>
<label for="email">Email Address:(Required)</label>
<input name="email"class="required email" id="email" type="text"
title="Please Enter a Valid email Address!">
</div>
 <div>
<label for="comment">Comment:</label>
<textarea rows="12" id="message" name="comment"></textarea> 
</div>
<div>
    <button type="submit" id="submit" value="Submit as Multipart/form-data">
        <img src="submit1.jpg" alt="Submit">
    </button>
</div>
<div>
    <button type="reset" id="reset">
        <img src="resetrs.jpg" alt="reset">
    </button>
</div>
   </fieldset>
 </form> 
 <script type="text/javascript" src="jqformval.js"></script>
 </body>
 </html>
PHP处理器:

<?php
    ini_set('display_errors',1);
/*stops undefined index error*/
if(isset($_POST['email'],$_POST['author'],$_POST['comment'] )) {

    $email = $_POST['email'];
    $author = $_POST['author'];
    $visitor_email = $_POST['email'];
    $comment = $_POST['comment'];
    $comment = wordwrap($comment, 70);
 }
     $email_from = 'xxxx@yyyy.com';
     $email_subject = "Form Submission";
     $email_body = "Message from $author.'n".
     "The message reads:'n $comment".
    $to = "xxxx@yyyy.com";
    $headers = "From: $email_from 'r'n";
    $headers .= "Reply-To: $visitor_email 'r'n";
    mail($to,$email_subject,$comment,$headers);
      header( "Location: Thank You.html" );
 ?>

尝试设置

$email = "";
$author = "";
$visitor_email = "";
$comment = "";

在你的脚本开头。

我也想知道即使没有提供信息,你是否打算发送电子邮件。如果是这种情况,那么

 $comment = $_POST['comment'];
 $comment = wordwrap($comment, 70);

应该移出if....如果你想只在所有信息都提供的情况下发送,那么你需要将其他所有内容移动到If

如何修复php文件的if条件…它可能有问题,试着修复它:-

    <?php
    ini_set('display_errors',1);
if(isset($_POST['submit'])) {
// and add the name="submit" to the submit button
    $email = $_POST['email'];
    $author = $_POST['author'];
    $visitor_email = $_POST['email'];
    $comment = $_POST['comment'];
    $comment = wordwrap($comment, 70);
 }