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

时间:2011-09-29 14:27:56

标签: php

当我在上传后在服务器端呈现它时,我收到一封电子邮件,但它是空白的。 From为空白,文本为空白,ECHO中$ name的部分为空白。我有什么想法可以做到这一点吗?

这是我的表格:

<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";


?>

3 个答案:

答案 0 :(得分:0)

From为空白,因为您已写入$headers = "From: $name\r\n";并且$name未在任何地方声明。将其更改为$headers = "From: $nameField\r\n";

对于$long(应为$selectField),$experience(应为$commentsField)和$visit(应为{ {1}})。

至于$visitField

  1. 为什么还要将HTML存储在字符串中然后将其回显?退出PHP模式并开始直接输出HTML。
  2. 为什么将变量echo "$theResults";括在引号中? $theResults更好,添加引号首先在内存中创建一个 new 字符串,然后回显它。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您的错误报告设置如何?它打印还是记录?回声phpinfo()看。你的日志会告诉你很多。

在下面的一行中,我注意到“mail”和“(”之间有一个空格。应该没有空格。

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

如果你正在观察你的错误日志,你会发现很容易错过的事情。