苦苦挣扎的PHP邮件功能

时间:2015-02-02 09:21:42

标签: php forms

我还是非常新的php所以我还在学习。有人可以用这个表格指出我正确的方向。我只想收到一封包含表格信息的电子邮件。它只是将我输入的值放回到表单字段中,不发送任何电子邮件。验证工作得很好,只是没有让邮件部分正确。

<!--Validation-->
<?php
// define variables and initialize with empty values
$nameErr = $addrErr = $emailErr = $howManyErr = $favFruitErr = "";
$name = $address = $email = $howMany = "";
$favFruit = array();
 
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["companyName"])) {
        $compNameErr = "Required";
    }
    else {
        $compName = $_POST["companyName"];
    }
	if (empty($_POST["name"])) {
        $nameErr = "Required";
    }
    else {
        $name = $_POST["name"];
    }
 
    if (empty($_POST["surname"])) {
        $surErr = "Required";
    }
    else {
        $surname = $_POST["surname"];
    }
	if (empty($_POST["tel"])) {
        $telErr = "Required";
    }
    else {
        $tel = $_POST["tel"];
    }
	
	if (empty($_POST["email"])) {
        $emailErr = "Required";
    }
    else {
        $email = $_POST["email"];
    }
	
	 if (empty($_POST["day"]))  {
        $dayErr = "Please choose Day 1 or Day 2";
    }
    else {
        $day = $_POST["day"];
    }
	
 
    if (!isset($_POST["spryradio1"])) {
        $timeslotErr = "You must select 1 option";
    }
    else {
        $timeslot = $_POST["spryradio1"];
    }
  }
   
  
 ?>


<form id="form1" name="form1" method="POST"
 action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  <p>
    <label for="companyName">Company Name</label>
    <input type="text" name="companyName" id="companyName" value="<?php echo htmlspecialchars($Compname);?>"/>
    <span class="error"><?php echo $compNameErr;?></span>
  </p>
  <p>Company Representative information:</p>
  <p>
    <label for="name">First Name</label>
    <input type="text" name="name" id="name" value="<?php echo htmlspecialchars($name);?>"/> 
    <span class="error"><?php echo $nameErr;?></span>
    
    <label for="surname">Last Name</label>
    <input type="text" name="surname" id="surname" value="<?php echo htmlspecialchars($surname);?>"/>
    <span class="error"><?php echo $surErr;?></span>
  </p>
  <p>
    <label for="tel">Tel No</label>
    <input name="tel" type="text" id="tel" size="15" maxlength="10" value="<?php echo htmlspecialchars($tel);?>"/>
    <span class="error"><?php echo $telErr;?></span></p>
    <p>
    	<label for="email">Email </label>
    <input type="text" name="email" id="email" value="<?php echo htmlspecialchars($email);?>" />
    <span class="error"><?php echo $emailErr;?></span>
  </p>
  <p>Would you like to do a radio/video interview on the day?  
    <label for="interview"></label>
    <select name="interview" id="interview">
      <option value="NULL"></option>
      <option>YES</option>
      <option>NO</option>
    </select>
  </p>
 
  <p>Please choose a time slot</p>
  <div id="spryradio1">
    <table width="700">
      <tr>
        <td><label for="day">Pick a Day</label>
    <select name="day" id="day" >
      <option></option>
      <option>Day 1</option>
      <option>Day 2</option>
    </select><span class="error"><?php echo $dayErr;?></span></td>
        <td style="background-color:#FFF;text-align:center; font-weight:bold; ">White</td>
        <td style="background-color:#000; color:#FFF; text-align:center; font-weight:bold; ">Black</td>
        <td style="background-color:#36F;text-align:center; font-weight:bold; ">Blue</td>
        <td style="background-color:#3C9;text-align:center; font-weight:bold; ">Green</td>
        <td style="background-color:#F30;text-align:center; font-weight:bold; ">Red</td>
        <td style="background-color:#FF9;text-align:center; font-weight:bold; ">Yellow</td>
        <td style="background-color:#F96;text-align:center; font-weight:bold; ">Orange</td>
      </tr>
      <tr>
        <td>06:00 - 09:00</td>
        <td style="background-color:#FFF;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_0" />
          W1</label></td>
        <td style="background-color:#000; color:#FFF;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_4" />
          BL1</label></td>
        <td style="background-color:#36F;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_8" />
          B1</label></td>
        <td style="background-color:#3C9;" ><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_12" />
          G1</label></td>
        <td style="background-color:#F30;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_16" />
          R1</label></td>
        <td style="background-color:#FF9;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_20" />
          Y1</label></td>
        <td style="background-color:#F96;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_24" />
          O1</label></td>
        </tr>
      <tr>
        <td>09:00 - 12:00</td>
        <td style="background-color:#FFF;" ><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_1" />
          W2</label></td>
        <td style="background-color:#000; color:#FFF"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_5" />
          BL2</label></td>
        <td style="background-color:#36F;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_9" />
          B2</label></td>
        <td style="background-color:#3C9;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_13" />
          G2</label></td>
        <td style="background-color:#F30;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_17" />
          R2</label></td>
        <td style="background-color:#FF9;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_21" />
          Y2</label></td>
        <td style="background-color:#F96;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_25" />
          O2</label></td>
        </tr>
      <tr>
        <td>12:00 - 15:00</td>
        <td style="background-color:#FFF;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_2" />
          W3</label></td>
        <td style="background-color:#000; color:#FFF;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_6" />
          BL3</label></td>
        <td style="background-color:#36F;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_10" />
          B3</label></td>
        <td style="background-color:#3C9;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_14" />
          G3</label></td>
        <td style="background-color:#F30;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_18" />
          R3</label></td>
        <td style="background-color:#FF9;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_22" />
          Y3</label></td>
        <td style="background-color:#F96;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_26" />
          O3</label></td>
        </tr>
      <tr>
        <td>15:00 - 18:00</td>
        <td style="background-color:#FFF;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_3" />
          W4</label></td>
        <td style="background-color:#000;color:#FFF;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_7" />
          BL4</label></td>
        <td style="background-color:#36F;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_11" />
          B4</label></td>
        <td style="background-color:#3C9;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_15" />
          G4</label></td>
        <td style="background-color:#F30;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_19" />
          R4</label></td>
        <td style="background-color:#FF9;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_23" />
          Y4</label></td>
        <td style="background-color:#F96;"><label>
          <input type="radio" name="Timeslot" value="radio" id="Timeslot_27" />
          O4</label></td>
        </tr>
      
    </table>
    <span style="color:#F00">Please make a selection.</span>  </div>
 <p>
   <input id="send" name="submit" type="submit" value="Submit">
<?php
if (isset($_POST["send"])) {

$subject = 'Booking - Million Meals';
$name = $_POST['name'];
$surname = $_POST['surname'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$day = $_POST['day'];
$timeslot = $_POST['spryradio1'];
$headers = 'From:'. $email . "\r\n"; // Sender's Email
// Send Mail By PHP Mail Function
mail("vaaljan@gmail.com", $subject, $name, $surname, $tel, $email, $day, $timeslot, $headers);
echo "Your mail has been sent successfuly! We will confirm your booking via email.";
}

?>


  </p>
</form>

2 个答案:

答案 0 :(得分:3)

您需要为邮件信息提供正文。

// Send Mail By PHP Mail Function
$msg = "Name : ".$name ."\n";
$msg .= "Surname : ".$surname ."\n";
$msg .= "Tel : ".$tel. "\n";
$msg .= "Email : ".$email ."\n"; 
$msg .= "Day : ".$day ."\n";
$msg .= "TimeSlot : ".$timeslot;

mail("vaaljan@gmail.com", $subject, $msg, $headers);

查看文档mail实际的电子邮件参数是这样的

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

我建议您使用一些库进行邮件,因为php mail()功能不可靠。试试PHPMailerSwiftMailer

答案 1 :(得分:0)

使用代码检查邮件功能是否启用

if (mail("vaaljan@gmail.com", $subject, $name, $surname, $tel, $email, $day, $timeslot, $headers)) {
echo "Your mail has been sent successfuly! We will confirm your booking via email."; }
else {
 echo "Fail";
}