2012-05-28 89 views
1

我有一個表格下面有必填字段,並在用戶填寫時通過電子郵件發送給公司。問題是,從電子郵件地址顯示爲我們的服務器名稱,是否有方法來指定來自地址是什麼?當發送表格時,從「電子郵件地址」聽寫

這些是控制數據的字段:

$to = the_field('email_address'); 
$subject = "UKHG contact enquiry"; 
$companyname_field = $_POST['companyname']; 
$fullname_field = $_POST['fullname']; 
$email_field = $_POST['email']; 
$tel_field = $_POST['tel']; 
$enquiry_field = $_POST['enquiry']; 

這裏的是一個完整的形式:

<div id="contactform"> 
<script language="JavaScript"> 
<!-- 

/*********************************************** 
* Required field(s) validation v1.10- By NavSurf 
* Visit Nav Surf at http://navsurf.com 
* Visit http://www.dynamicdrive.com/ for full source code 
***********************************************/ 

function formCheck(formobj){ 
    // Enter name of mandatory fields 
    var fieldRequired = Array("fullname", "email", "tel", "enquiry"); 
    // Enter field description to appear in the dialog box 
    var fieldDescription = Array("Name", "Email", "Telephone", "Enquiry"); 
    // dialog message 
    var alertMsg = "Please complete the following fields:\n"; 

    var l_Msg = alertMsg.length; 

    for (var i = 0; i < fieldRequired.length; i++){ 
     var obj = formobj.elements[fieldRequired[i]]; 
     if (obj){ 
      switch(obj.type){ 
      case "select-one": 
       if (obj.selectedIndex == 0 || obj.options[obj.selectedIndex].text == ""){ 
        alertMsg += " - " + fieldDescription[i] + "\n"; 
       } 
       break; 
      case "select-multiple": 
       if (obj.selectedIndex == -1){ 
        alertMsg += " - " + fieldDescription[i] + "\n"; 
       } 
       break; 
      case "text": 
      case "textarea": 
       if (obj.value == "" || obj.value == null){ 
        alertMsg += " - " + fieldDescription[i] + "\n"; 
       } 
       break; 
      default: 
      } 
      if (obj.type == undefined){ 
       var blnchecked = false; 
       for (var j = 0; j < obj.length; j++){ 
        if (obj[j].checked){ 
         blnchecked = true; 
        } 
       } 
       if (!blnchecked){ 
        alertMsg += " - " + fieldDescription[i] + "\n"; 
       } 
      } 
     } 
    } 

    if (alertMsg.length == l_Msg){ 
     return true; 
    }else{ 
     alert(alertMsg); 
     return false; 
    } 
} 
// --> 
</script> 
<form class="form" onsubmit="return formCheck(this);" method="POST" action="<?php the_permalink(); ?>"> 
    <table border="0" style="float:left;"> 
     <tbody> 
      <tr> 
       <td> 
       <p>Company Name:</p> 
       </td> 
       <td>&nbsp;</td> 
       <td><input type="text" name="companyname" id="companyname" /></td> 
      </tr> 
      <tr> 
       <td> 
       <p>Your Name<span style="color:red;">*</span>:</p> 
       </td> 
       <td>&nbsp;</td> 
       <td><input type="text" name="fullname" id="fullname" /></td> 
      </tr> 
      <tr> 
       <td> 
       <p>E-mail<span style="color:red;">*</span>:</p> 
       </td> 
       <td>&nbsp;</td> 
       <td><input type="text" name="email" id="email" /></td> 
       <td colspan="2">&nbsp;</td> 
      </tr> 
      <tr> 
       <td> 
       <p>Telephone<span style="color:red;">*</span>:</p> 
       </td> 
       <td>&nbsp;</td> 
       <td><input type="text" name="tel" id="tel" /></td> 
       <td colspan="2">&nbsp;</td> 
      </tr> 
      <tr> 
       <td valign="top"> 
       <p>Enquiry<span style="color:red;">*</span>:</p> 
       </td> 
       <td>&nbsp;</td> 
       <td><textarea name="enquiry"></textarea></td> 
       <td colspan="2">&nbsp;</td> 
      </tr> 
      <tr> 
       <td colspan="4"> 
        <button class="blue medium awesome awesomeforward" type="submit" name="submit">Send message</button>    
       </td> 
      </tr>    
     </tbody> 
    </table> 

<? if(isset($_POST['submit'])) { 

$to = the_field('email_address'); 
$subject = "UKHG contact enquiry"; 
$companyname_field = $_POST['companyname']; 
$fullname_field = $_POST['fullname']; 
$email_field = $_POST['email']; 
$tel_field = $_POST['tel']; 
$enquiry_field = $_POST['enquiry']; 


$body = "Hello,\n\n You have an enquiry from the website, please see the details below:\n\n Name: $fullname_field\n Company Name: $companyname_field\n E-Mail: $email_field\n Tel: $tel_field\n Message:\n $enquiry_field\n\n Please reply to the enquiry asap.\n\n Kind Regards \n"; 

mail($to, $subject, $body); 

echo "</br>Thank you for getting in touch, we will contact you shortly."; 

} ?> 
</form> 
</div> 

回答

2

你可以通過一個額外的頭設置在PHP郵件命令:

$header = 'From: [email protected]'; 
mail($to, $subject, $body, $header); 
相關問題