开发者

Why is my PHP script sending blank emails?

开发者 https://www.devze.com 2023-04-09 14:21 出处:网络
When I render it on the server side after uploading, I get an email but it is blank. The From is blank the text is blank the part in ECHO where $name is comes out blank.Any ideas what I can do to make

When I render it on the server side after uploading, I get an email but it is blank. The From is blank the text is blank the part in ECHO where $name is comes out blank. Any ideas what I can do to make this work?

Here is my form:

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

And my PHP code

<?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><b开发者_C百科r>
  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 is blank because you have written $headers = "From: $name\r\n"; and $name is not declared anywhere. Change this to $headers = "From: $nameField\r\n";.

The same can be said for references to $long (should be $selectField), $experience (should be $commentsField) and $visit (should be $visitField).

As for echo "$theResults";:

  1. Why bother storing the HTML in a string to then echo it out? Just drop out of PHP mode and start outputting the HTML directly.
  2. Why surround the variable $theResults in quotes? echo $theResults; is better, adding the quotes creates a new string in memory first, then echos it.


Your are echoing fields that you did not declare... try adding extract($_POST); to the begining of your code


How is your error reporting set? Does it print or go to a log? Echo phpinfo() to see. Your log will tell you a lot.

In the following line I noticed that you have a space between "mail" and "(". There should be no space.

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

If you are watching your error log you will catch things like this that are easy to miss.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号