2014-10-08 169 views
0

以下代碼負責使用Mail()函數發送郵件。我面臨的問題是收到電子郵件時,沒有任何內容(換句話說,電子郵件中沒有顯示任何數據)。我測試了所有功能,並且它們工作正常。郵件功能不發送

<style> 
#ajax_form {width:410px;font-family:verdana,arial;font-size:12px} 
#ajax_form td{font-family:verdana,arial;font-size:12px} 
#ajax_form_header {font-family:verdana,arial;font-size:1.3em;font-weight:bold;text-align:center} 
#returned_value{font-family:verdana,arial;text-align:center;font-size:12px;color:#000000} 
#go {border:1px solid #CCCCCC;background:#FFF} 
</style> 
<script type="text/javascript" src="cform.js"></script> 

<div id="ajax_form"> 
<form> 
<div id="ajax_form_header">Contact Us Form</div> 
<br /> 
<table width="350" border="0" align="center" cellpadding="4" cellspacing="0"> 
    <tr> 
    <td><label>Your Name:</label></td> 
    <td><input type="text" id="name" style="width:100%" /></td> 
    </tr> 
    <tr> 
    <td><label>Your Email:</label></td> 
    <td><input type="text" id="email" style="width:100%" /></td> 
    </tr> 
    <tr> 
    <td><label>Your Subject:</label></td> 
    <td><input type="text" id="subject" style="width:100%" /></td> 
    </tr> 
    <tr> 
    <td colspan="2"> 
     <label>Your Message:</label><br /><br /> 
     <textarea name="body" style="width:100%;height:160px" id="body"></textarea> 
    </td> 
    </tr> 
    <tr align="center"> 
    <td colspan="2"><input type="button" value="Submit" id="submit" onClick="return check_values();"></td> 
    </tr> 
</table> 
</form> 
    <br /> 
    <div align="center"><!-- leave this link please --><a href="http://www.freecontactform.com/ajax_form.php">Ajax Contact Form</a></div><br /><br /> 
    <div id="confirmation" style="display:none" align="center"></div> 
</div> 

cform.js代碼:

var http = createRequestObject(); 
var areal = Math.random() + ""; 
var real = areal.substring(2,6); 

function createRequestObject() { 
    var xmlhttp; 
    try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); } 
    catch(e) { 
    try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");} 
    catch(f) { xmlhttp=null; } 
    } 
    if(!xmlhttp&&typeof XMLHttpRequest!="undefined") { 
    xmlhttp=new XMLHttpRequest(); 
    } 
    return xmlhttp; 
} 

function sendRequest() { 
    var rnd = Math.random(); 
    var name = escape(document.getElementById("name").value); 
    var email = escape(document.getElementById("email").value); 
    var subject = escape(document.getElementById("subject").value); 
    var body = escape(document.getElementById("body").value); 

    try{ 
    http.open('POST', 'pform.php'); 
    http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
    http.onreadystatechange = handleResponse; 
     http.send('name='+name+'&email='+email+'&subject='+subject+'&body='+body+'&rnd='+rnd); 
    } 
    catch(e){} 
    finally{} 
} 

function check_values() { 
    var valid = ''; 

    var name = document.getElementById("name").value; 
    var email = document.getElementById("email").value; 
    var subject = document.getElementById("subject").value; 
    var body = document.getElementById("body").value; 
    if(trim(name) == "" || 
     trim(email) == "" || 
     trim(subject) == "" || 
     trim(body) == "") { 
      alert("Please complete all fields"); 
    } else { 
     if(isEmail(email)) { 
      document.getElementById("submit").disabled=true; 
      document.getElementById("submit").value='Please Wait..'; 
      sendRequest(); 
     } else { 
      alert("Email appears to be invalid\nPlease check and try again"); 
      document.getElementById("email").focus(); 
      document.getElementById("email").select(); 
     } 
    } 
} 

function handleResponse() { 
    try{ 
    if((http.readyState == 4)&&(http.status == 200)){ 
     var response = http.responseText; 
     document.getElementById("confirmation").innerHTML = response; 
     document.getElementById("confirmation").style.display =""; 
     } 
    } 
    catch(e){} 
    finally{} 
} 

function isUndefined(a) { 
    return typeof a == 'undefined'; 
} 

function trim(a) { 
    return a.replace(/^s*(S*(s+S+)*)s*$/, "$1"); 
} 

function isEmail(a) { 
    return (a.indexOf(".") > 0) && (a.indexOf("@") > 0); 
} 

pform.php文件:

include 'cform_config.php'; 

if(!isset($rnd) || !isset($name) || !isset($email) || !isset($subject) || !isset($body)) { 
    echo $error_message; 
    die(); 
} 

    $email_from = $email; 
    $email_subject = "Contact Form: ".stripslashes($subject); 
    $email_message = "Please find below a message submitted by '".stripslashes($name); 
    $email_message .="' on ".date("d/m/Y")." at ".date("H:i")."\n\n"; 
    $email_message .= stripslashes($body); 

    $headers = 'From: '.$email_from."\r\n" . 
    'Reply-To: '.$email_from."\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

    mail($email_it_to, $email_subject, $email_message, $headers); 

    echo "<b>$confirmation</b>"; 
    die(); 

cform_config.php文件:

$page_title = "Contact Us Form"; 
    $email_it_to = "[email protected]_domain.com"; 
    $error_message = "Please complete the form first"; 
    $confirmation = "Thank you, your message has been successfully sent."; 

我跟蹤check_values()功能具有以下sendRequest()功能,我發現沒有什麼不對的

+0

'$ email_message。= stripslashes($ body);'what' $ body' ?? – 2014-10-08 20:55:25

回答

3

$email_message .= stripslashes($body);應該是
$email_message .= stripslashes($email_message);$body不存在。

(我不知道你爲什麼在這裏或者在你的代碼中的任何地方使用stripslashes(),它不應該是必須的)。

+0

下劃線?爲什麼? – 2014-10-08 20:56:40

+0

@Dagon完全無意。 – 2014-10-08 20:57:21

+0

只是檢查,當有人像你這樣做,即時通訊認爲有一個原因,而不是一個錯字 – 2014-10-08 20:57:59

0

正如@Dagon所說,問題出在$_POST['email']。因此,我必須在pform.php文件的開頭添加此部分。

相關問題