2013-11-22 125 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。很高兴帮助你! –