2013-11-22 176 views
0

我的聯繫表單有問題。當我點擊提交按鈕時,PHP頁面加載,但PHP消息不顯示,並且不發送電子郵件。PHP表單到電子郵件[郵件()功能]不起作用

這裏是我的表單代碼:

<form name="Contact" method="post" action="form_to_email.php"> 
    <table width="100%" border="0" cellpadding="0" cellspacing="20"> 
     <tr> 
     <td width="25%"><em>Full name:</em></td> 
     <td width="75%"><input type="text" name="FName" id="FName" accesskey="F" placeholder="First name" required autocomplete="on"> 
     <input type="text" name="LName" id="LName" accesskey="L" placeholder="Last name" required autocomplete="on"></td> 
     </tr> 
     <tr> 
     <td><em>E-mail address:</em></td> 
     <td><input name="Email" type="text" id="Email" placeholder="[email protected]" accesskey="E" size="25" required autocomplete="on"></td> 
     </tr> 
     <tr> 
     <td><em>Subject:</em></td> 
     <td><select name="Subject" id="Subject" accesskey="S"> 
      <option value="Comment">Comment</option> 
      <option value="Question">Question</option> 
     </select></td> 
     </tr> 
     <tr> 
     <td><em>Comments/Questions:</em></td> 
     <td><textarea name="Comments" id="Comments" cols="45" rows="5" accesskey="C" required title="Please type your comment or question here."></textarea></td> 
     </tr> 
     <tr> 
     <td>&nbsp;</td> 
     <td><input type="submit" name="Submit" id="Submit" value="Submit"></td> 
     </tr> 
    </table> 
</form> 

PHP代碼(form_to_email.php)是:

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

// EDIT THE 2 LINES BELOW AS REQUIRED 
$email_to = "*****@hotmail.com"; 
$email_subject = "Form Mail"; 


function died($error) { 
    // your error code can go here 
    echo "We are very sorry, but there were error(s) found with the form you submitted. "; 
    echo "These errors appear below.<br /><br />"; 
    echo $error."<br /><br />"; 
    echo "Please go back and fix these errors.<br /><br />"; 
    die(); 
} 

// validation expected data exists 
if(!isset($_POST['FName']) || 
    !isset($_POST['LName']) || 
    !isset($_POST['Email_from']) || 
    !isset($_POST['Subject']) || 
    !isset($_POST['Comments'])) { 
    died('We are sorry, but there appears to be a problem with the form you submitted.');  
} 

$fname = $_POST['FName']; // required 
$lname = $_POST['LName']; // required 
$email_from = $_POST['Email_from']; // required 
$subject = $_POST['Subject']; // not required 
$comments = $_POST['Comments']; // required 

$error_message = ""; 
$email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
if(!preg_match($email_exp,$email_from)) { 
$error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 
} 
$string_exp = "/^[A-Za-z .'-]+$/"; 
if(!preg_match($string_exp,$fname)) { 
$error_message .= 'The First Name you entered does not appear to be valid.<br />'; 
} 
if(!preg_match($string_exp,$lname)) { 
$error_message .= 'The Last Name you entered does not appear to be valid.<br />'; 
} 
if(strlen($comments) < 2) { 
$error_message .= 'The Comments you entered do not appear to be valid.<br />'; 
} 
if(strlen($error_message) > 0) { 
died($error_message); 
} 
$email_message = "Form details below.\n\n"; 

function clean_string($string) { 
    $bad = array("content-type","bcc:","to:","cc:","href"); 
    return str_replace($bad,"",$string); 
} 

$email_message .= "First Name: ".clean_string($fname)."\n"; 
$email_message .= "Last Name: ".clean_string($lname)."\n"; 
$email_message .= "Email: ".clean_string($email_from)."\n"; 
$email_message .= "Subject: ".clean_string($subject)."\n"; 
$email_message .= "Comments: ".clean_string($comments)."\n"; 


// create email headers 
$headers = 'From: '.$email_from."\r\n". 
'Reply-To: '.$email_from."\r\n" . 
'X-Mailer: PHP/' . phpversion(); 
mail($email_to, $email_subject, $email_message, $headers); 
?> 

<!-- include your own success html here --> 

    Thank you for contacting us. We will be in touch with you very soon. 

<?php 
} 
?> 

這可能是我使用的服務器有問題?默認的PHP變量在這裏http://www.freewebhostingarea.com/phpinfo-default_variables.html,除了我不明白什麼頁面說什麼..

也通過成員區域我可以配置幾個PHP變量。它對新用戶說,safe_mode會在頭幾個小時內自動關閉。其他缺省值爲:register_globals爲ON,magic_quotes_gpc爲OFF,allow_url_include爲OFF,short_open_tag爲OFF。

+2

您的表單輸入稱爲'Email',但您正在尋找'$ _POST ['email']' - 此案例需要匹配。 – andrewsi

+0

UNRELATED你應該在這裏添加一些垃圾郵件防護,無論是驗證碼,還是一些隨機算術問題,例如1 + 2等等,以便自動化代碼不能發佈和(發送)給你發送電子郵件。 – nrathaus

+0

@andrewsi是的,我解決了它謝謝你:) – CoffeeBubble

回答

0

在你形成了,你已經使用的名稱Email輸入

<input name="Email" type="text" id="Email" placeholder="[email protected]" accesskey="E" size="25" required autocomplete="on">

但在form_to_email.php,你已經使用如下

if(isset($_POST['email'])) { 

} 

所以它應該是,

if(isset($_POST['Email'])) { 

} 

以及$email_from = $_POST['Email_from']; sho將其更改爲$email_from = $_POST['Email'];

+0

謝謝你的工作!我認爲這個問題比那個更加複雜。再次,謝謝:) – CoffeeBubble

+0

NP。很高興幫助你! –