为什么我的PHP脚本发送空白电子邮件


Why is my PHP script sending blank emails?

当我上传后在服务器端渲染时,我收到一封电子邮件,但它是空白的。From是空的,文本是空的,ECHO中$name is的部分是空的。你知道我该怎么做吗?

这是我的表格:

<form action="survey.php" method="post" name="survey">
        <table>`enter code here`
        <tr><th colspan="2">Survey</th></tr>
        <tr><td>Name:</td><td align="center"><input type="text"
name="name" id="name" /></td></tr>
        <tr><td>When was your last visit</td><td
align="center"><select id="long" name="long">
        <option value="Select">Please Select</option>
    <option value="1-2 days">1-2 days ago</option>
    <option value="3-5 days">3-5 days ago</option>
    <option value="1-2 weeks">1-2 weeks ago</option>
    <option value="3-5 weeks">3-5 weeks ago</option>
    <option value="2 months">2 months</option>
    <option value="3+months">3+ months </option>
    <option value="Never">Never Visited</option>
    </select></td></tr>
    <tr>
    <td>Tell us about your experience?</td><td
align="center"><textarea cols="22" rows="5" id="experience"
name="experience"></textarea></td></tr>
    <tr><td>Will you come back
    to visit again?</td><td align="center"><input type="checkbox"
id="visit" name="visit" value="Yes" />Yes<input type="checkbox"
id="visit" name="visit" value="No" />No</td></tr>
    <tr><td colspan="2"><input type="submit" value="Submit" /><input
type="reset" value="Reset" /></td></tr>
    </table>
    </form>

和PHP代码

<?php 
/* subject and e-mail variables*/   
$emailSubject = $name;  
$webMaster = 'patrick@patrickspcrepair.com'; 
/* Gathering data Variables */  
$nameField = $_POST['name'];    
$selectField = $_POST['long'];
$commentsField = $_POST['experience'];
$visitField = $_POST['visit'];
$body = <<<EOD
  <br><hr><br>
  Name: $name<br>
  Date of last visit: $long<br>
  Expierence: $experience<br>
  Visit again: $visit<br>
EOD;
$headers = "From: $name'r'n";   
$headers .= "Content-type: text/html'r'n";  
$success = mail ($webMaster, $emailSubject, $body, $headers); 
/* Results rendered as HTML */
/* When changing the fields please leave the <<<EOD and the EOD;
If they are deleted this form will not work */  
$theResults = <<<EOD
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"
  />
  <title>Coupon</title>
  <script language="javascript" type="text/javascript">
  var d=datetime.getDate()
  var datetime=new Date()
  var day=datetime.getDay()
  var month=datetime.getMonth()
  var year=datetime.getYear()
  if (year < 1000)
  year+=1900
  var days=new
  Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
  var months=new
  Array("January","February","March","April","May","June","July","August","September","Octomber","November","December")
  </script> 
  <style type="text/css">
  #frame {
  position:absolute;
  top:16px;
  left:124px;
  height:355px;
  width:480px;
  border:2px dotted red;
  font:"Trebuchet MS", Veranda, sans-serif;
  color:red;
  padding: 10px;
  }
  </style>
  </head>
  <body>
  <div id="frame">
  <h3 align="center">Thanks for Participating in our survey,
  $name!</h3>
  <p align="center">Here is a special offer for you:</p>
  <h2 align="center">Free Egg Roll!</h2>
  <p align="center">Please print out this coupon to recieve a free
  egg roll with the purchase of a meal.</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p style="font-size:x-small">Limit one coupon per visit. Coupon
  valid at all locations. Coupon will expire one month after date on
  coupon. Cannot be combined with other offers or promotions. Management
  has the right to refuse this coupon if found to be altered, copied, or
  fraudulent. Coupon Number : <script>

  function fakecounter(){
    //decrease/increase counter value (depending on perceived
    popularity of your site!)
    var decrease_increase=50000
    var counterdate=new Date()
    var currenthits=counterdate.getTime().toString()
    currenthits=parseInt(currenthits.substring(2,currenthits.length-4))+decrease_increase
    document.write(" # <b>"+currenthits+"</b>")
  }
  fakecounter()
  </script></p>
  </div>
  <script language="JavaScript" type="text/javascript">
  document.write(days[day]+", "+months[month]+" "+d+" "+year)
  </script>
  <form><input type="button" value=" Print this page "
  onclick="window.print();return false;" /></form> 
  </body>
  </html>
EOD;
echo "$theResults";

?>

From为空,因为您写了$headers = "From: $name'r'n";$name没有在任何地方声明。修改为$headers = "From: $nameField'r'n";

对于$long(应该是$selectField), $experience(应该是$commentsField)和$visit(应该是$visitField)的引用也是如此。

对于echo "$theResults";:

  1. 为什么要把HTML存储在字符串中然后回显出来?退出PHP模式,直接输出HTML。
  2. 为什么用引号包围变量$theResults ?echo $theResults;更好,添加引号首先在内存中创建一个新的字符串,然后返回它。

您正在返回未声明的字段…尝试将extract($_POST);添加到代码的开头

如何设置错误报告?它是打印还是进入日志?回显phpinfo()查看。你的日志会告诉你很多。

在下一行中,我注意到在"mail"answers"("之间有一个空格。不能有空格

$success = mail ($webMaster, $emailSubject, $body, $headers);

如果你正在查看你的错误日志,你会发现像这样的东西很容易错过